附加的事件处理程序“元数据”在哪里?存储?在“DOM”上,对象,或者……?
我一直想知道......所以你有这样的代码:
$('#click-me');
然后将其附加到:
$('#click-me').click(someFunction);
“元数据”在哪里,上面写着:
“嘿“jQuery-object #click-me,”我当您被点击时,将指向“someFunction”!”
我知道事件处理程序可能会被破坏,例如我在 Backbone.js 中的情况,由于我重新渲染整个页面,我的事件停止触发,破坏了一些背景一路上的函数/对象/视图..(这是我问这个问题的上下文)
现在,我的问题是:
在哪里事件“元数据”存储以及如何销毁? 它们是否存储在将其绑定到函数的函数中?它们是否在 DOM“元数据”(如果有的话)本身内?
我正在尝试学习 JavaScript 的复杂性,因为我厌倦了错误。除此之外,我想知道是否应该注意可能会分离我的事件等的垃圾收集。来自 C#,我想说带有 DOM 的 JavaScript 真的很厉害......
(另外,作为旁注,我如何访问这些事件并“调试”它们?firefox?chrome?)
更新
换句话来说,连接 DOM 元素和某个事件的信息存储在哪里? DOM?物体? (或者.. jQuery 映射它吗?JavaScript 有“元数据”吗? 它围绕该上下文..
I've always wondered... so you have a code like this:
$('#click-me');
and you attach it with this:
$('#click-me').click(someFunction);
where is the 'meta-data' that says:
"Hey "jQuery-object #click-me," I will point you to 'someFunction' when you are clicked!"
I know that event-handlers can get destroyed such as my situation with Backbone.js where my events stopped firing due to me re-rendering the entire page, destroying some background functions/objects/Views along the way.. (this is the context as to why I'm asking this question)
NOW, MY QUESTION IS:
where are events 'meta-data' stored and how are they destroyed?
Are they stored within the function that bound it to a function? Are they within the DOM 'meta-data' (if there is one) itself?
I'm trying to learn the intricacies of JavaScript because I'm tired of bugs. In addition to that, I'm wondering if I should watch out for garbage collection that might detach my events and such. Coming from C#, I would say JavaScript with the DOM is really something...
(also, as a side note, how can I access these events and 'debug' them? firefox? chrome?)
UPDATE
To say it in different words, where is the information that connects a DOM element to a certain event stored? DOM? Objects? (or.. does jQuery map it? does JavaScript have a 'meta-data'? it's around that context..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新:所以我误解了这个问题,你想知道事件如何在 javascript 和 html 的上下文中绑定。下面我原来的答案描述了 jquery 如何创建和管理事件。它归结为对 element.addEventListener 的调用。
从 MDN 文档中您可以看到事件目标可以是元素、文档、窗口或 XMLHttpRequest。来自 关于 DOM 事件的 w3 规范 事件目标添加、删除和分派事件。因此,即使信息可能存储在封装元素之类的内容中,这也将在浏览器级别实现。
从您提到的有关复制然后替换正文中的 html 的问题中,会删除事件,我认为浏览器只是为您提供标记(没有事件元数据),然后当您替换它时,元数据就消失了。 ( http://jsfiddle.net/qu9bF/1/)
原始答案:jquery 事件处理程序如何工作。
好的,所以我开始针对 JQuery 1.4.2 进行挖掘(因为我必须使用几个工具,所有这些工具都没有更新)
首先看一下:
http://james.padolsey.com/jquery/#v= 1.4.2&fn=click
这就是 click 的定义方式,它实际上并不是在代码中定义的。 JQuery 为所有事件/处理函数定义了这个函数,如下所示,是的!它们是动态创建/定义的:
从这里开始,我们需要查看绑定,现在
bind()
和one()
也被定义为这。在此处搜索“代码:绑定和一个事件” a>从这里我使用 chrome 和这个小提琴 http://jsfiddle.net/qu9bF/ 单步执行代码。
c.each(["bind"
) 中的块是定义绑定函数的方式。源代码已缩小,但是 chrome 可以对其进行格式化。从这里开始,代码调用
JQuery.events.add
,您可以在事件部分这里。这不是记录的add()
我认为在底部,这一段代码是有魔力的。它相应地调用 element.addEventListener 或 attachEvent。了解它如何添加on 用于
attachEvent
。现在你就拥有了! :) 我希望它能回答你的两个问题。您可以链接到 jquery 源代码的非缩小版本,并逐步浏览它以找出问题所在。 IMO 有时 IE9 的调试器更直观(这是我使用它的唯一用途),并且使用我提到的页面来理智地浏览源代码。
Update : So I misunderstood the question, you wanted to know how events are bound in the context of just javascript and html. My original answer below describes how jquery creates and manages events. It boils down to a call to element.addEventListener.
From the MDN docs you see the eventtarget can be an element, the document, window or an XMLHttpRequest. From the w3 specifications on DOM Events an event target adds, removes and dispatches an event. So even information is probably stored in whatever is encapsulating things like elements, this will be implemented at the browser level.
From the issue you mentioned about copying and then replacing the html from the body erases the events, I'm thinking the browser just gives you the markup (without the event metadata) and then when you replace it, the metadata is gone. ( http://jsfiddle.net/qu9bF/1/)
Original answer: How jquery event handlers work.
Ok so I started digging this, for JQuery 1.4.2 (because I had to use a couple tools, all of which aren't updated)
Take a look first at this:
http://james.padolsey.com/jquery/#v=1.4.2&fn=click
That is how click is defined, it isn't actually defined in code. JQuery defines this function for all events/handler functions like below, yes! they are created/defined dynamically :
From here on we need to look at bind, now
bind()
andone()
are also defined like this. Search for "Code : bind and one events" hereFrom here I used chrome with this fiddle http://jsfiddle.net/qu9bF/ to step into the code. The block from
c.each(["bind"
is how the bind function is defined. The source is minified, but chrome can format it.From here on the code calls
JQuery.events.add
, you can find this under the Events section here. This is not theadd()
that is documented I thinkToward the bottom, this piece of code is what does the magic. It accordingly calls element.addEventListener or attachEvent. See how it adds the on for
attachEvent
.And there you have it! :) I hope it answered both your questions. You can link up to the non-minified versions of jquery source and step through it to figure things out. IMO sometimes IE9's debugger is more intuitive (that's the only thing I use it for), and use the pages I've mentioned to browse through the source sanely.
jQuery 将所有事件绑定和数据缓存存储在 jQuery.cache 对象上。当您使用 jQuery
html
、empty
、remove< 时,所有用 jQuery 包装并绑定了事件或数据集的 DOM 节点将被自动清除。 /code>、
replace
等。这就是为什么永远不要使用
innerHTML
或其他本机 DOM 方法来插入/替换之前由 jQuery 更改的内容非常重要的原因。除非您手动重置 jQuery.cache 对象,否则它将导致您无法清除的泄漏。还有一个未记录的方法
jQuery.cleanData
,它将 DOM 节点集合作为参数,迭代它们并清理它们的所有事件绑定、数据并从缓存中删除对这些元素的引用。如果您有与主 DOM 树分离的 DOM 片段,并且存在无法正确清理它们的风险,则此选项可能会很有用。jQuery stores all the event binding and data cache on the jQuery.cache object. All the DOM nodes which were wrapped with jQuery and had events bound to them or data set will get automatically cleared when you are using jQuery
html
,empty
,remove
,replace
etc.That's why it's very important to never use
innerHTML
or other native DOM methods to insert/replace content that was altered before by jQuery. It will lead to leaks that you won't be able to cleanup unless you reset the jQuery.cache object manually.There is also an undocumented method
jQuery.cleanData
which takes a collection of DOM nodes as an argument and it iterates over them and cleans up all their event bindings, data and removes references to these elements from the cache. This one can be useful if you have DOM fragments which were detached from the main DOM tree and there is a risk that they won't get cleaned up properly.像点击或提交这样的常规事件(当不受 jQuery 约束时)实际上只是 DOM 元素本身的属性(“onclick”、“onsubmit”)。
对于 jQuery 事件,当您绑定它们时,库会保留它自己的记录,并在您触发它们时查看它。 jQuery 将有关元素的所有数据放在一个标准位置,您可以使用
$(e).data()
访问该位置。对于事件,它只是$(e).data('events')
。您可以使用
$().unbind()
取消绑定 jQuery 事件,并使用delete
关键字来删除与给定事件对应的对象属性的常规事件。Regular events like click or submit (when not bound by jQuery) are actually just properties ('onclick', 'onsubmit') of the DOM elements themselves.
For jQuery events, the library keeps it's own record when you bind them and looks at it when you trigger them. jQuery puts all data about elements in a standard place, which you can access with
$(e).data()
. For events, it's just$(e).data('events')
.You can unbind jQuery events with
$().unbind()
, and regular events using thedelete
keyword to delete the object's property which corresponds to the given event.jQuery 保留自己的元素事件处理程序映射。除非您以某种方式滥用图书馆,否则很少,非常罕见,有理由担心这一点。
jQuery keeps its own map of element event handlers. There's rarely, very rarely, cause to worry about this unless you're abusing the library somehow.