[英]How can I remove (.remove) all elements (div) but not this (this) element (div) in jquery?
I have a set of divs listening for a click. 我有一组div听一下。 When one is clicked I need to remove all the others.
点击一个我需要删除所有其他人。
HTML: HTML:
<div class="foo">A</div>
<div class="foo">B</div>
<div class="foo">C</div>
<div class="foo">D</div>
If I click div (A), how can I remove all the rest (B,C,D) but keep A? 如果我点击div(A),我如何删除所有其余的(B,C,D)但保留A?
I tried this, but it didn't work: 我试过这个,但它不起作用:
$('.foo:not(this)').remove();
$('.foo').not(this).remove(); //w00t
So to put it all together: 所以把它们放在一起:
$('.foo').click(function(){
//code...
$('.foo').not(this).remove(); //w00t
//more code....
});
IF you are trying to remove all the siblings of a set of elements, you can do this. 如果您试图删除一组元素的所有兄弟,您可以这样做。
$(".foo").click(function (e) {
$(this).siblings().remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.