事件泡沫问题(我猜......!)
我有一个大 DIV #bigdiv。当我将其悬停时,我希望在大 div 内显示一个信息丰富的小 DIV #info。问题:当我将鼠标放在 #info 上时(一旦我通过悬停 #bigdiv 使其出现),它会使其 (#info) 无限地出现和消失。这是我的代码:
jQuery('#bigdiv').live("hover", function(){
jQuery("#info").toggle();
});
更新
我尝试了 mashappslabs 的解决方案(mouseenter/mouseleave),并且尝试了 TJ Crowder 的解决方案(不使用 live()):结果相同。
在这两种情况下,小 div 仍然会出现并永远消失。
I have a big DIV #bigdiv. When I hover it, I want a small informative DIV #info to appear inside the big div. Problem: when I put my mouse on #info (once I made it appear by hovering #bigdiv) it makes it (#info) appear and disappear infinitely. Here's my code :
jQuery('#bigdiv').live("hover", function(){
jQuery("#info").toggle();
});
Update
I tried mashappslabs's solution (mouseenter/mouseleave), and I tried T.J. Crowder's one (not to use live()) : same result.
In both cases the small div still shows up and disappear forever.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它看起来像是
hover
的live
版本中的一个小错误。将使用live
的此示例与这个 实际上在bigdiv
上使用事件处理程序(计数器在那里,所以我们知道事情何时发生变化)。问题是当您将鼠标移入info
div(单词“info”)并移出它(仍在bigdiv
内)时。因此,答案可能是不要使用live
,尽管我猜测您有使用live
的理由,因此这可能会带来不便。您可能知道
hover
基本上只是mouseenter
和mouseleave
的组合(它们是 IE 特定的事件,但 jQuery 在浏览器上模拟它们不要提供它们)。但是mouseenter
和mouseleave
不会冒泡;这就是它们有用的部分原因。不过,相关的(而不是特定于 IE 的)mouseover
和mouseout
事件会冒泡,因此这看起来可能是特定于委托处理的mouseenter
/mouseleave
模拟。 (编辑:具体来说,bug #9069,已报告并正在积极开展工作。)It looks like a minor bug in the
live
version ofhover
. Contrast this example, usinglive
, with this one using an event handler actually onbigdiv
(the counter is there so we know when things are changing). The issue is when you move the mouse into theinfo
div (the word "info") and out of it (still withinbigdiv
). So the answer may be not to uselive
, although I'm guessing you have a reason for usinglive
and so that may be inconvenient.You may know that
hover
is basically just a combination ofmouseenter
andmouseleave
(which are IE-specific events, but jQuery emulates them on browsers that don't provide them). Butmouseenter
andmouseleave
don't bubble; that's part of why they're useful. The related (and not IE-specific)mouseover
andmouseout
events bubble, though, so this looks like itmight beis a bug in themouseenter
/mouseleave
emulation that's specific to delegated handling. (Edit: Specifically, bug #9069, which has already been reported and is being actively worked on.)@glabus
使 #info 位置绝对,
您也可以使用 jquery.css 动态设置顶部和左侧
@glabus
make #info position absolute
you can dynamically set top and left with jquery.css also
使用 mouseenter 和 mouseleave,查看 http://jsfiddle.net/2aCJ2/ 的工作示例,这里是代码:
CSS:
HTML:
jQuery:
希望有帮助。干杯
Use mouseenter and mouseleave, check working example at http://jsfiddle.net/2aCJ2/ and here is the code:
CSS:
HTML:
jQuery:
Hope it helps. Cheers