How do you eliminate an all divs of a child class but one?
I have the following DOM structure:
<div class="city">
<div class="a">Delete</div>
<div class="b">Selected</div>
<div class="c">Delete</div>
</div>
How do you save city and di开发者_JS百科v class b, but delete div class a and c if you know that the user has selected "b" (you know this using logs your console.log(variable))
Basically what JQuery method would you use to do this and how?
$('.city').children('div').not('.b').remove();
Given your structure, I'd probably use the following:
$(".b").siblings().remove();
See it in action: http://jsbin.com/akuno3/edit
$('.city > div:not(.b)').remove();
$('.city').find('.a, .c').remove();
Or
$('.city div:not(.b)').remove();
With class variable:
var selectedClass = 'b';
$('.city div').not('.' + selectedClass).remove();
Try this:
$("div.city div.b").click(function() {
$(this).siblings().remove()
})
Basically I would do:
$(".b").click(function () {
$(".a,.c").remove();
});
精彩评论