jquery delegate() 事件(鼠标悬停、鼠标悬停)触发两次
我有以下脚本,总是触发鼠标悬停和鼠标移出两次! 你建议我做错了什么(解除绑定,返回例如)?我尝试了一些事情但是 没有任何帮助。
这是代码:
$('#container').delegate('div.showmenu', 'mouseover mouseenter mouseout mouseleave', function(e){
if (e.type === 'mouseover' || e.type==='mouseenter') { //jIE requires mouseenter, does not fire mouseover
if($(this).parents().closest('div').hasClass('whatever')){
alert(e.type); //double-alerts mouseover
menu.show();
foldercmenu.hover(
function(){
$(this).show();
},
function(){
$(this).hide();
}
);
}else {
//do other stuff :-)
}
}else if(e.type==='mouseout' || e.type==='mouseleave'){ //IE requires mouseleave, does not fire mouseout
alert(e.type); //double-alerts mouseout
menu.hide();
$(this).unbind('mouseover mouseenter mouseout mouseleave');
}
//return false;
});
i have the following script firing mouseover and mouseout always twice!
what do you suggest i do wrong (unbind, return e.g.)? i tried a few things but
nothing helped.
here is the code:
$('#container').delegate('div.showmenu', 'mouseover mouseenter mouseout mouseleave', function(e){
if (e.type === 'mouseover' || e.type==='mouseenter') { //jIE requires mouseenter, does not fire mouseover
if($(this).parents().closest('div').hasClass('whatever')){
alert(e.type); //double-alerts mouseover
menu.show();
foldercmenu.hover(
function(){
$(this).show();
},
function(){
$(this).hide();
}
);
}else {
//do other stuff :-)
}
}else if(e.type==='mouseout' || e.type==='mouseleave'){ //IE requires mouseleave, does not fire mouseout
alert(e.type); //double-alerts mouseout
menu.hide();
$(this).unbind('mouseover mouseenter mouseout mouseleave');
}
//return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您进入/离开元素的子元素时,会触发
mouseover
和mouseout
,也许这就是您看到的效果。另一个问题是您将处理程序绑定到
mouseover
和mouseenter
(以及mouseleave
和鼠标移开
)。仅绑定到
mouseenter
和mouseleave
。 jQuery 已经解决了浏览器的差异。mouseover
andmouseout
a triggered when you enter/leave a child of the element, maybe that's the effect you are seeing.An other problem is that you are binding the handler to both,
mouseover
andmouseenter
( andmouseleave
andmouseout
).Only bind to
mouseenter
andmouseleave
. jQuery is already taking care of the browser differences.我有类似的东西 .delegate();
您检查过 http://api.jquery.com/event.stopImmediatePropagation/ 吗?
它解决了我的问题。
I had something similar with .delegate();
Have you checked out http://api.jquery.com/event.stopImmediatePropagation/?
It fixed my issue.