[英]How can i remove all div's and other DOM HTML elements except a particular class using jQuery
[英]How can I remove (.remove) all elements (div) but not this (this) element (div) in jquery?
我有一组div听一下。 点击一个我需要删除所有其他人。
HTML:
<div class="foo">A</div>
<div class="foo">B</div>
<div class="foo">C</div>
<div class="foo">D</div>
如果我点击div(A),我如何删除所有其余的(B,C,D)但保留A?
我试过这个,但它不起作用:
$('.foo:not(this)').remove();
$('.foo').not(this).remove(); //w00t
所以把它们放在一起:
$('.foo').click(function(){
//code...
$('.foo').not(this).remove(); //w00t
//more code....
});
如果您试图删除一组元素的所有兄弟,您可以这样做。
$(".foo").click(function (e) {
$(this).siblings().remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.