简体   繁体   English

如何仅使用JavaScript,不使用框架来访问绑定到dom元素的事件

[英]How do I access what events have been bound to a dom element using only JavaScript, no frameworks

How do I access what events have been bound to a dom element using JavaScript and NOT using a library/framework or firefox add-on? 如何使用JavaScript而不是使用库/框架或Firefox附加组件访问绑定到dom元素的事件? Just pure JavaScript. 只是纯JavaScript。 I incorrectly assumed there would be an events object stored as a property of the element which has the binding. 我错误地认为将有一个事件对象存储为具有绑定的元素的属性。

For example if I had bound say, click, dblclick and mouseover to an element how would I do the following NOT using jQuery. 例如,如果我不得不说,单击,dblclick和将鼠标悬停在某个元素上,该如何不使用jQuery进行以下操作。 Just JavaScript. 只是JavaScript。

function check(el){
 var events = $(el).data('events');
 for (i in $(el).data('events')) {
  console.log(i) //logs click dblclick and mouseover
  }
}

I know jQuery stores an events object as a data property ie $(el).data('events') and the eventbug add-on displays the event binding so there must be way. 我知道jQuery将事件对象存储为数据属性,即$(el).data('events') ,并且eventbug附加组件显示事件绑定,因此必须有办法。

I will also add that this question came about because I read about memory leaks in older IE browsers and how it's best to remove the bound events before removing a node from the DOM, which lead me to think, how can I test for what events are bound to an element? 我还将添加这个问题是因为我了解了旧版IE浏览器中的内存泄漏以及在从DOM中删除节点之前最好先删除绑定事件,这使我思考,如何测试哪些事件是绑定到元素?

Many thanks in advance;) 提前谢谢了;)

You can't reliably know what listeners are on an element if you aren't in complete control of the environment. 如果您不能完全控制环境,则无法可靠地知道元素上的侦听器。 Some libraries manually control event listeners, eg jQuery stores them in an object and adds a custom attribute to the element so that when an even occurs, it uses the custom property to look up the listeners for that element and event and calls them. 一些库手动控制事件侦听器,例如jQuery将事件侦听器存储在对象中,并向元素添加自定义属性,以便在出现偶数时,它使用custom属性查找该元素和事件的侦听器并调用它们。

Try: 尝试:

<div id="d0">div 0</div>
<script type="text/javascript">

var el = document.getElementById('d0')
el.addEventListener('click',function(){alert('hey');},false);

// Make sure listener has had time to be set then look at property
window.setTimeout(function(){alert(el.onclick)}, 100); // null

</script>

So to know what listeners have been added, you need to be in complete control. 因此,要知道已添加了哪些侦听器,您需要完全控制。

Edit: 编辑:

To make it clear, there is no reliable way to inspect an element with javascript and discover what listeners have been added, or even if any have been added at all. 明确地说,没有可靠的方法来使用javascript检查元素并发现添加了哪些侦听器,甚至根本没有添加任何侦听器。 You can create an event registration scheme to track listeners added using your event registration methods. 您可以创建事件注册方案,以跟踪使用事件注册方法添加的侦听器。 But if listeners are added some other way (eg directly by addEventListener) then your registration scheme won't know about them. 但是,如果以其他方式添加了侦听器(例如,直接通过addEventListener添加),则您的注册方案将不了解它们。

As pointed out elsewhere, jQuery (and others) can't track listeners that are added directly to elements without using the jQuery event registration methods (click, bind, mouseover, etc.) because they use those methods to register (and call) the listeners. 正如在其他地方指出的那样,jQuery(和其他人)无法跟踪不使用jQuery事件注册方法(单击,绑定,鼠标悬停等)而直接添加到元素的侦听器,因为它们使用这些方法来注册(并调用)听众。

// list of onevent attributes; you'll have to complete this list
var arr = ['onclick', 'onmouseover', 'onmouseup'];

for ( var i = 0; i < arr.length; i++ ) {
    if ( el[arr[i]] != null ) {
        // element has an arr[i] handler
    }  
}

where el is a reference to your DOM element 其中el是对DOM元素的引用

Complete lists are here 完整列表在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM