简体   繁体   English

向addEventListener注册的事件存储在哪里?

[英]Where is an event registered with addEventListener stored?

I start with an empty page. 我从空白页开始。 If I run document.body.onclick I get null . 如果我运行document.body.onclick ,则为null If I apply following code: 如果我应用以下代码:

document.body.onclick = function() { return "Click"; };

I get function() { return "Click"; } 我得到function() { return "Click"; } function() { return "Click"; } when I run document.body.onclick . function() { return "Click"; }当我运行document.body.onclick That makes sense! 那讲得通! But when I run 但是当我跑步时

document.body.addEventListener("click", function() { return "Click"; });

document.body.onclick is still null , but the output is "Click" when I run document.body.click() . document.body.onclick仍然为null ,但是运行document.body.click()时输出为"Click"

So my question is, where is the function stored when using addEventListener ? 所以我的问题是,使用addEventListener时函数存储在哪里?

Its stored in the actual list (array) of Event listeners for body . 它存储在bodyEvent侦听器的实际列表(数组)中。

Elements have a list of function references in them for their event listeners. 元素中有一个事件引用的函数引用列表。 These references are not in the DOM. 这些引用不在DOM中。 When firing an event, the browser has to run thru all the appropriate elements looking for these references and running them in order. 触发事件时,浏览器必须通过所有适当的元素来运行,以查找这些引用并按顺序运行它们。

Anyhow... There is plenty going on in the background that the DOM does not see. 无论如何...在DOM中看不到的事情很多。 The entire event system is one of them. 整个事件系统就是其中之一。 And well, the whole Javascript engine essentially and with a large object tree for the current loaded page, all stored in mysterious memory. 而且,整个Javascript引擎基本上都具有一个大对象树,用于当前加载的页面,所有这些都存储在神秘的内存中。 They are generally accessed by using the document and window interfaces, just like the DOM. 与DOM一样,通常可以通过使用documentwindow接口来访问它们。 But properly registered events will be in this large object tree that's stored in memory, which is not the same as the DOM. 但是正确注册的事件将在存储在内存中的这个大对象树中,该树与DOM不同。 Just closely related. 只是紧密相关。 I view the DOM as an interface or middle-man between this large object tree and the HTML itself. 我将DOM视为这种大对象树和HTML本身之间的接口或中间人。

Moving on... onclick is limited to just one value or a single string of javascript just sitting there in the DOM. 继续... onclick仅限于一个值或仅位于DOM中的一串JavaScript。 Not actually registered. 尚未实际注册。 So, not an actual Event listener. 因此,不是实际的事件侦听器。 Here the DOM is kind of like a shim in that it props up the onclick strings to run as events. 此处的DOM就像是垫片一样,它支撑了onclick字符串以作为事件运行。 So that this string also gets run when the event is fired. 这样,在触发事件时也可以运行此字符串。 By the browser, at the right time, mysteriously. 在正确的时间,由浏览器神秘地显示。 In a round about way, it could be said that is part of what the DOM does in general, it shims in all the in-line text like this from HTML, so that its accessible by document . 总的来说,这可以说是DOM的一部分,它可以填充HTML中的所有此类内联文本,以便document可以访问。 But generally, they are just stored as strings instead of actual objects in the tree. 但是通常,它们只是作为字符串存储,而不是存储在树中的实际对象。 This is probably one of many reasons why the DOM is so foobar. 这可能是DOM如此愚蠢的许多原因之一。

Whereas addEventListenener actually registers it as a real event, thus you can: addEventListenener实际上将其注册为真实事件,因此您可以:

  • Have multiples 有倍数
  • Access them as objects 作为对象访问它们
  • Add and remove them at will 随意添加和删除它们
  • Select an event propagation scheme 选择事件传播方案
  • Control event propagation itself during runtime 在运行时控制事件本身的传播
  • Have the event auto remove itself after first run 第一次运行后让事件自动删除

    ... and a few other features. ...和其他一些功能。

They are both, sort-of, two different event listeners for the same event. 它们都是同一事件的两个不同的事件侦听器。 One is a full event listener when using addEventListener . 一个是使用addEventListener时的完整事件侦听器。 And the other is just a string of text sitting in the DOM that the browser will run "at the right time", but not a actual full event listener. 另外一个只是位于DOM中的文本字符串,浏览器将在“正确的时间”运行它,而不是实际的完整事件侦听器。

This question might shed some light... addEventListener vs onclick 这个问题可能会引起一些启发 ... addEventListener vs onclick

The main difference between .onclick = and .addEventListener is that .onclick sets an event handler replacing the existing one. 之间的主要区别.onclick =.addEventListener.onclick集的事件处理程序替换现有之一。 Instead of that .addEventListener adds more than one event handlers to a DOM Element. 而不是.addEventListener可以向DOM元素添加多个事件处理程序。 .addEventListener is a modern way to add event handlers. .addEventListener是添加事件处理程序的现代方法。 Where they are stored, I have no idea, but not in onclick because onclick keeps a single value, not an array of event handlers. 我不知道它们的存储位置,但不知道onclick是因为onclick保留单个值,而不是事件处理程序数组。 You can read more about this here: 您可以在此处了解更多信息:

https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener#Older_way_to_register_event_listeners https://developer.mozilla.org/zh-CN/docs/DOM/element.addEventListener#Older_way_to_register_event_listeners

http://www.w3.org/TR/DOM-Level-3-Events/#events-DocumentEvent-createEvent http://www.w3.org/TR/DOM-Level-3-Events/#events-DocumentEvent-createEvent

So the answer is that you can not get the event handler added by .addEventListener by getting the value of onlick . 因此,答案是,你不能获得通过添加事件处理程序.addEventListener通过获取的价值onlick

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

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