悬停事件会冒泡吗?
我的父 DIV 将有许多可单击的子项,因此使用事件委托,我尝试捕获子项在父 DIV 上冒泡时的单击和悬停事件。
现在点击事件冒泡正常,但我似乎无法捕获悬停事件。有人知道为什么吗?
请不要告诉我悬停事件不会冒泡:)
HTML:
<div id="theParent">
<a href="#">Clicked 1</a>
<a href="#">Clicked 2</a>
<a href="#">Clicked 3</a>
<a href="#">Clicked 4</a>
<a href="#">Clicked 5</a>
</div>
jQuery:
$('#theParent').click(captureEvent).hover(captureEvent,captureEvent);
var captureEvent = function(e){
console.log(e.target);
};
My parent DIV will have many clickable children, so using event delegation I'm trying to capture both the childrens click and hover events on the parent DIV as they bubble up.
Now the click event bubbles fine, but I can't seem to capture the hover event. Anyone know why?
Please don't tell me the hover event doesn't bubble :)
HTML:
<div id="theParent">
<a href="#">Clicked 1</a>
<a href="#">Clicked 2</a>
<a href="#">Clicked 3</a>
<a href="#">Clicked 4</a>
<a href="#">Clicked 5</a>
</div>
jQuery:
$('#theParent').click(captureEvent).hover(captureEvent,captureEvent);
var captureEvent = function(e){
console.log(e.target);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用之前尝试定义
captureEvent
— 似乎有效:http://jsfiddle.net/ rEzSd/1/ 。但是,如果#theParent
的所有空间都被链接占据,则悬停的目标将是链接。我添加了一些填充以使其清晰。Try defining
captureEvent
before using it — seems to work: http://jsfiddle.net/rEzSd/1/ . The target for the hover will however be a link if all space of#theParent
is occupied by links. I've added some padding to make that clear.