[英]i want to remove the parent of the parent of 'this' in jquery
I have a span .icon-trash
in a div parent in another div parent I want when I click it to remove .item-append
and I have many of the .item-append
我在另一个 div 父级的 div 父级中有一个 span
.icon-trash
我想要当我单击它以删除.item-append
并且我有许多.item-append
<div class="item-append">
<div class="cont1">
<img src="">
</div>
<div class="cont2">
<span class="qua">
<span class="icon-trash"></span>
</span>
</div>
</div>
I tried jQuery but a don't know what should I put in the selector我试过 jQuery 但不知道应该在选择器中输入什么
$('.icon-trash').on('click', function () {
$(selector).remove();
});
To remove the .item-append
element when the .icon-trash
element is clicked, you can use the following code:要在单击
.icon-trash
元素时删除.item-append
元素,可以使用以下代码:
$('.icon-trash').on('click', function() {
$(this).closest('.item-append').remove();
});
In the code above, this refers to the .icon-trash
element that was clicked.在上面的代码中,this 指的是被点击的
.icon-trash
元素。 The closest()
method is used to find the nearest ancestor element that matches the given selector (in this case, .item-append
). closest()
方法用于查找与给定选择器匹配的最近祖先元素(在本例中为.item-append
)。
Alternatively, you could also use the following code to achieve the same result:或者,您也可以使用以下代码来获得相同的结果:
$('.icon-trash').on('click', function() {
$(this).parent().parent().remove();
});
In this case, the parent()
method is used twice to move up the DOM tree from the .icon-trash
element to its parent .cont2
element, and then to its parent .item-append
element, which is then removed.在这种情况下,使用了两次
parent()
方法将 DOM 树从.icon-trash
元素向上移动到其父.cont2
元素,然后移动到其父.item-append
元素,然后将其删除。
If you just want to remove the class form the .item-append
, try this如果你只想从
.item-append
中删除 class ,试试这个
$('.icon-trash').on('click', function () {
$(this).closest('.item-append').removeClass('item-append');
});
https://api.jquery.com/removeclass/ https://api.jquery.com/removeclass/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.