gpt4 book ai didi

javascript - 如何将相同的 jQuery 代码应用于同一类的两个实例?

转载 作者:行者123 更新时间:2023-11-30 07:17:58 25 4
gpt4 key购买 nike

例如我有这段代码:

$('.note .exit').click(function() {
$('.note').fadeOut();
});

在我的 HTML 上有两个 .exit 和 .note 实例。如:

<div class="note">
<img src="images/exit.png" class="exit"/>

<p>Some text.</p>
</div>

<div class="note">
<img src="images/exit.png" class="exit"/>

<p>Some other text.</p>
</div>

问题是当我单击一个 .note 的关闭按钮(退出图像)时,另一个 .note 也会关闭。如何修改代码,使其仅适用于我要关闭的特定 .note 类?

最佳答案

使用.closest() [docs]获取与选择器匹配的最近祖先:

$('.note .exit').click(function() {
$(this).closest('.note').fadeOut();
});

鉴于您的结构,您还可以使用 .parent() [docs] .

关于javascript - 如何将相同的 jQuery 代码应用于同一类的两个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7322089/

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