捕获父元素上的事件
当单击发生时,window.event 报告树中最低的元素。例如,
<a href="http://server.com/page.aspx" id="anchor">
<span id="span" class="Someclass">
<strong id="strong">Submit</strong>
</span> </a>
当您单击上面的链接时,如果您在 BODY 级别有 onclick 监听器,则 window.event 将报告“强”元素被单击。在同一事件中,我如何知道父级实际上是一个“锚”标签,其 href 指向某处?即我正在寻找一种自动方式让我知道主要元素是“锚”而不是“强”元素。 我显然无法执行 window.event.target.parentNode.parentNode 因为这将是手动检查。可能在某个地方我可以跟踪点击事件的“冒泡”。
有人有任何线索吗?
window.event reports the lowest element in the tree when a click happens. e.g.
<a href="http://server.com/page.aspx" id="anchor">
<span id="span" class="Someclass">
<strong id="strong">Submit</strong>
</span> </a>
When you click on the above link and if you have onclick listner at BODY level, the window.event will report that "strong" element got clicked. In the same event, how do I know that the parent is actually an "anchor" tag which has href pointing somewhere? i.e. I am looking for an automatic way for me to know the main element is "anchor" and not "strong" element.
I clearly can't do window.event.target.parentNode.parentNode as this will be manual checking. May be somewhere I can follow the "bubbling" of the click event.
Anyone any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以自动检查祖先。
大多数库都有一个内置的方法来执行此操作。例如,YUI 3 有 Node .ancestor,而 jQuery 有最接近的。
You can check the ancestors automatically.
Most libraries have a method to do this built in. e.g. YUI 3 has Node.ancestor, and jQuery has closest.