事件委托的事件冒泡
我正在尝试使用以下代码实现事件委托:
<html>
<head>
<script>
document.addEventListener('click', function(e){
alert(e.target.className);
}, false);
</script>
</head>
<body>
<ul class="list">
<li class="item">A</li>
<li class="item">B</li>
<li class="item">C</li>
<li class="item">D</li>
</ul>
</body>
</html>
我希望事件冒泡,以便当我单击
项目时,警报会显示 item
,然后另一个警报显示 list
。但此代码仅给出带有 item
的警报。我该如何修改它以使事件冒泡?I am trying to implement event delegation with the following code:
<html>
<head>
<script>
document.addEventListener('click', function(e){
alert(e.target.className);
}, false);
</script>
</head>
<body>
<ul class="list">
<li class="item">A</li>
<li class="item">B</li>
<li class="item">C</li>
<li class="item">D</li>
</ul>
</body>
</html>
I want the event to bubble so that, when I click on an <li>
item, an alert shows item
, and then another alert shows list
. But this code only gives the alert with item
. How can I modify it so that the event bubbles?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事件回调只触发一次,但并不代表事件不会冒泡。仅当该元素侦听事件时才会执行回调。
如果您确实希望警报一直触发到祖先,请在回调中一一添加祖先的警报。您不必向所有祖先添加侦听器,这会导致消耗更多内存。
Event callback is only triggered once, but it does not mean the event does not bubble. The callback is only executed when the very element listens to the event.
If you really want the alert to be triggered all the way back to the ancestors, in your callback, add the alerts for the ancestors one by one. You dont have to add listeners to all the ancestors, which would cause consuming more memory.
您需要为列表添加事件来提醒它。事件由父对象捕获,但没有事件绑定到它们,因此它不会发出警报。您需要为所有对象添加事件来提醒它。
You need to add event for list to alert it. Event is captured by parent objects but there is no event bound to them so it doesn't alert. You need to add event for all objects to alert it.
当您在
document
对象上绑定点击处理程序时,您不能期望它会被多次调用。它只会被调用一次(在document
对象处)。顺便说一句,事件确实在 DOM 树中冒泡。如果没有,它就不会到达
document
对象,并且您不会看到任何警报框。因此,事件在 LI 元素上触发,然后一直冒泡到
document
对象,在该对象中调用单击处理程序。顺便说一句,通常您不会一直委托给
document
对象,而是委托给最接近的共同祖先。例如,如果您想处理 LI 元素上的点击,则委托给 UL 元素:Live demo: http://jsfiddle.net/rCVfq/
When you bind a click handler at the
document
object, you can't expect it to be invoked multiple times. It will be invoked only once (at thedocument
object).Btw the event does bubble up the DOM tree. If it didn't, it wouldn't reach the
document
object, and you wouldn't see any alert box.So, the event fires at the LI element, and then bubbles all the way up to the
document
object, where the click handler is invoked.Btw usually you don't delegate all the way up to the
document
object, but to the closest common ancestor. For example, if you want to handle clicks on the LI elements, you delegate to the UL element:Live demo: http://jsfiddle.net/rCVfq/
我刚刚发布了一个小型事件委托库,以便更轻松地执行此类操作。在 http://craig.is/riding/gators 上查看
您可以这样做
这将绑定一个单击文档的处理程序并将相应地分派事件。
当您单击
li
时,它们都会触发,除非您停止传播。I just released a small event delegation library to make it easier to do things like this. Check it out at http://craig.is/riding/gators
You can do
This will bind a single click handler to the document and will dispatch the events accordingly.
They will both fire when you click an
li
unless you stop propagation.您可以使用某种循环方法来调用这两个警报:
You can use some looping method to invoke both alerts: