停止 Javascript 中的事件冒泡
我有一个像这样的 html 结构:
<div onmouseover="enable_dropdown(1);" onmouseout="disable_dropdown(1);">
My Groups <a href="#">(view all)</a>
<ul>
<li><strong>Group Name 1</strong></li>
<li><strong>Longer Group Name 2</strong></li>
<li><strong>Longer Group Name 3</strong></li>
</ul>
<hr />
Featured Groups <a href="#">(view all)</a>
<ul>
<li><strong>Group Name 1</strong></li>
<li><strong>Longer Group Name 2</strong></li>
<li><strong>Longer Group Name 3</strong></li>
</ul>
</div>
我希望仅从主 div 触发 onmouseout
事件,而不是 div 中的“a”或“ul”或“li”标签!
我的 onmouseout
函数如下:
function disable_dropdown(d)
{
document.getElementById(d).style.visibility = "hidden";
}
有人可以告诉我如何阻止事件冒泡吗?我尝试了其他网站上提供的解决方案(stopPropogatio
等),但我不确定如何在这种情况下实现它们。
任何帮助将不胜感激。
I have a html structure like :
<div onmouseover="enable_dropdown(1);" onmouseout="disable_dropdown(1);">
My Groups <a href="#">(view all)</a>
<ul>
<li><strong>Group Name 1</strong></li>
<li><strong>Longer Group Name 2</strong></li>
<li><strong>Longer Group Name 3</strong></li>
</ul>
<hr />
Featured Groups <a href="#">(view all)</a>
<ul>
<li><strong>Group Name 1</strong></li>
<li><strong>Longer Group Name 2</strong></li>
<li><strong>Longer Group Name 3</strong></li>
</ul>
</div>
I want the onmouseout
event to be triggered only from the main div, not the 'a' or 'ul' or 'li' tags within the div!
My onmouseout
function is as follows:
function disable_dropdown(d)
{
document.getElementById(d).style.visibility = "hidden";
}
Can someone please tell me how I can stop the event from bubbling up? I tried the solutions (stopPropogatio
etc) provided on other sites, but I'm not sure how to implement them in this context.
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您真正想要使用的事件是 onmouseenter 和 onmouseleave< /strong>,但并非所有浏览器都实现了它们。您可以自己实现它们,但是在这种情况下,您最好使用已经为您解决跨浏览器问题的库。所以,在 jQuery 中
The events that you really want to use are onmouseenter and onmouseleave, however they are not implemented in all browsers. You could look to implement them yourself, however you would in this case be better off using a library that has already solved the problem cross browser for you. So, in jQuery