gpt4 book ai didi

jquery-selectors - jquery - 如何选择元素和所有子元素,特定子元素除外

转载 作者:行者123 更新时间:2023-12-01 02:55:15 25 4
gpt4 key购买 nike

我在“节点”类的所有元素上触发了 .mouseover() 事件。它工作正常,当用户将鼠标悬停在 .node 的任何子节点上时也会触发。这对我来说效果很好,但是,如果用户鼠标悬停在特定的 child (即图像)上,我不希望触发此事件。

为什么选择器 $('.node :not(img)') 不工作?

奇怪的是,当我在 http://www.woods.iki.fi/interactive-jquery-tester.html 试用时它确实有效。 ,但在我的实际实现中没有。

我的html:

    <div id="container">
<div class="node" id="abc">
<h2>Node Title</h2>
<div class="nodeChildren">
<h3 id="a1">Some text</h3>
<h3 id="a2">Some text</h3>
<h3 id="a3">Some text</h3>
</div>
<img src="diagonal-left.gif" />
</div>
</div>

我的jQuery:
    //start with third tier not visible
$('.nodeChildren').hide();

$('.node :not(img)').mouseover(function(){
$(this).children('div').show();
});

$('.node').mouseout(function() {
$('.nodeChildren').hide();
});

});

我失败的尝试
$('.node :not(img)') //no mouseover is triggered

//for the following selectors, the mouseover event is still triggered on img
$('.node').not('img')
$('.node').not($('.node').children('img'))

谢谢你的帮助 :)

最佳答案

问题是事件“冒泡”到触发鼠标悬停的 parent 身上。您需要向 img 添加鼠标悬停以阻止此操作。

$(".node img").bind("mouseover", function(event) {
event.stopPropagation();
});

关于jquery-selectors - jquery - 如何选择元素和所有子元素,特定子元素除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3209680/

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