jquery - 仅将鼠标悬停在 1 个具有相同 jquery 类的元素上

标签 jquery jquery-selectors hover css-selectors

我在 jquery 中有这段代码:

$(".pun").hover(function() {
 $(".guns").show(0);        
});

我的html

<div class="pun">
<div class="guns">gun 1</div>
</div>
<div class="pun">
<div class="guns">gun 2</div>
</div>
<div class="pun">
<div class="guns">gun 3</div>
</div>

我的CSS

.pun {
display:visible
}

.guns {
display:none
}

我想要执行悬停操作,仅显示正在悬停的项目。现在,如果您将鼠标悬停,则会显示每个 div 枪支。

最佳答案

要仅显示您悬停的 pun div 内的 guns div,请使用 find 函数

$(".pun").hover(function() {
   $(this).find(".guns").show(0);        
});

或者为您的选择器提供上下文

$(".pun").hover(function() {
   $(".guns", this).show(0);        
});

此外,show 的默认设置不执行动画,因此您可以将 .show(0); 替换为 show();

关于jquery - 仅将鼠标悬停在 1 个具有相同 jquery 类的元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8670834/

相关文章:

javascript - 在同一上下文中具有其他 JS 函数的 Jcrop 插件

javascript - jquery如何在运行时获取数据?

jquery hasClass 匹配元素

javascript - 脚本没有创建跨度

html - 图像边框悬停不起作用

html - CSS:带有子元素过渡的动画悬停

jquery - 如何使用 JQuery 更改 image_tag 助手的来源?

jquery - 如何在使用 jQuery 切换按钮时隐藏所有按钮?

javascript - 可选的 div 背景颜色

jquery - 使用选择器加速此 jQuery 代码