gpt4 book ai didi

jquery - 单击时删除 DIV

转载 作者:行者123 更新时间:2023-12-03 22:12:59 24 4
gpt4 key购买 nike

我有一个 DIV,当我单击该 DIV 中包含的链接时,我想将其删除。这是我所拥有的:

<div id="clients-edit-wrapper">
<div class="close-wrapper">
<a href="#" class="close-div">Close</a>
</div>
</div>

当我单击“关闭”时,我希望删除 clients-edit-wrapper。我正在寻找一种方法来通过引用 Close 链接的父 DIV 来执行此操作,在本例中为 clients-edit-wrapper

任何帮助将不胜感激!

<小时/>

黄教的回答如下:

$('.close-div').click(function(){
$(this).parent().parent().remove();
});

仅当您要删除的元素有两个父元素时,此方法才有效。就我而言,这正是我所需要的。

最佳答案

给定您的 html 标记

更新为.on()

$('.close-div').on('click', function(){
$(this).closest("#clients-edit-wrapper").remove();
});

通过.closest获得更大的灵 active ,这使您可以选择拥有更多的 parent 或更少的 parent 。

https://api.jquery.com/closest/

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

编辑
(已添加相关资源)
请参阅 live() 上的 jQuery 文档

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

据我所知,这是由于 live() 的内存问题/问题。

关于jquery - 单击时删除 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11057970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com