我正在编写一个脚本,我想在其中处理鼠标事件,前提是它们之前没有被任何其他元素处理过。
我可以将事件监听器附加到文档对象,但它会接收所有事件,无论它们是否已被处理。
我无法控制 HTML 页面中的元素,因此我无法在处理事件时手动停止传播()。
有什么想法吗?
最佳答案
来自这篇文章 here .
似乎还不可能做到这一点。
Which event handlers are registered?
One problem of the current implementation of W3C’s event registration model is that you can’t find out if any event handlers are already registered to an element. In the traditional model you could do:
alert(element.onclick)
and you see the function that’s registered to it, or undefined if nothing is registered. Only in its very recent DOM Level 3 Events W3C adds an
事件监听列表
to store a list of event handlers that are currently registered on an element. This functionality is not yet supported by any browser, it’s too new. However, the problem has been addressed.
Fortunately
removeEventListener()
doesn’t give any errors if the event listener you want to remove has not been added to the element, so when in doubt you can always use removeEventListener().
关于javascript - 处理其他元素未处理的 Javascript 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484634/