jQuery-是否选择除单个元素及其子元素以外的所有内容? [英] jQuery - Select everything except a single elements and its children?
本文介绍了jQuery-是否选择除单个元素及其子元素以外的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试去除整个体内的所有东西,除了单个元素及其子元素.我该怎么做?
I'm trying to remove everything in the whole body, except of a single element and its children. How can I accomplish this?
编辑1 考虑以下标记:
<html>
<body>
<div id="div1"></div>
<div id="div2">
<div id="elementNotToRemove"></div>
</div>
<div id="div3"></div>
<div id="div4"></div>
</body>
</html>
在上面的示例中,我要删除div1,div3和div4及其子级,但是保留div2,因为它包含不删除的元素.
In the above example, I want to remove div1, div3 and div4, as well as their children, but conserve div2 because it contains the element not to remove.
推荐答案
$("body > *").not("body > #elementtokeep").remove();
您可以用想要保留的内容替换not()部分
You can replace the not() part with whatever you want to keep
这篇关于jQuery-是否选择除单个元素及其子元素以外的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文