gpt4 book ai didi

jquery - 单击子 Div 时仅隐藏 1 个父级

转载 作者:行者123 更新时间:2023-12-01 03:09:40 25 4
gpt4 key购买 nike

尝试使用 .closest() 时遇到问题。

我的页面上有一个重复的父/子 div,当我单击子项时,我希望隐藏父项。但只有被点击的 child 的 parent 。

现在发生的情况是页面上的所有家长都被隐藏了。

代码如下:HTML

<div class="parent">
<a href="#" class="child">I'm the child</a>
</div>
<div class="parent">
<a href="#" class="child">I'm the child</a>
</div>
<div class="parent">
<a href="#" class="child">I'm the child</a>
</div>

JQuery

$( ".child" ).click(function() {
$( ".child" ).closest( ".parent" ).fadeOut();
});

你知道我做错了什么吗?提前致谢!

最佳答案

使用 $(this) 引用被单击的元素。

$(".child").click(function() {
$(this).closest(".parent").fadeOut();
});

关于jquery - 单击子 Div 时仅隐藏 1 个父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712295/

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