我在类‘节点’的所有元素上触发了一个.mouseover()事件。它工作正常,并且在用户悬停在.node的任何子节点上时也会触发。这对我来说很好,但是,如果用户鼠标超过了一个特定的子程序,即图像,我不希望触发这个事件。
为什么选择器$('.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'))(谢谢你的帮助:)
发布于 2010-07-09 03:50:18
问题是,这个事件是“冒泡”到家长的触发鼠标。您需要在img中添加一个鼠标以阻止这种情况。
$(".node img").bind("mouseover", function(event) {
event.stopPropagation();
});发布于 2010-07-09 04:00:57
在bind的回调函数中,可以检查目标。在你的情况下,像这样的事情
$(".node").bind('mouseover',function(e){
if(e.target.nodeName != 'IMG'){
$(this).children('div').show();
}
});https://stackoverflow.com/questions/3209680
复制相似问题