Jquery 从所选html中排除特定div
我有一个类似这样的htmlJquery 从所选html中排除特定div,jquery,Jquery,我有一个类似这样的html <div class="modal-content"> <div class="modal-header"> // Header Content Here </div> <div class="modal-footer"> // Footer Content Here </div> <div class="modal-footer-
<div class="modal-content">
<div class="modal-header">
// Header Content Here
</div>
<div class="modal-footer">
// Footer Content Here
</div>
<div class="modal-footer-after">
// Footer Content Here
</div>
</div>
<div class="modal-header">
// Header Content Here
</div>
<div class="modal-footer-after">
// Footer Content Here
</div>
上面的代码返回给我整个html,包括模式页脚。我错过了什么?试试这个
$(function(){
var html = '';
$(".modal-content div").not(".modal-footer").each(function(){
html+= $(this).html();
})
alert(html);
});
.不是你想的那样。它从当前集合中删除匹配项
您可以克隆集,然后删除页脚:
var formhtml = $(".modal-content").clone()
//Find and remove your footer
.find('.modal-footer').remove()
//then return to last element set and get the html.
.end().html();
试一试
不在jquery中使用
$(".modal-content").children(":not('.modal-footer')").clone();
你的答案也很有效,但这是一个较长的过程。请看乔治的答案。没有了它们,它工作得很好。谢谢你的帮助,我真的很感激。我不确定George method是fast还是我的,但他正在经历克隆、查找、删除,然后结束,然后获取html。但正如他的名声所反映的那样,他必须是正确的,或者是最好的方式。在所描述的那种一次性情况下,速度差并不重要。两个答案都可以更快,但也没那么重要。这不会提供所需的HTML字符串。您是否假设它们将附加子元素?这很可能是一个选项JSFIDLE demo?类似于:$'body'.append.append$.modal content>:not'.modal footer'.clone;注意我简化了你的selectors@TrueBlueAussie很抱歉反应太晚,谢谢你的宝贵意见
$(".modal-content").children(":not('.modal-footer')").clone();