我看到jQueryXXX ="YY"属性添加到我的某些DOM元素中 [英] I am seeing jQueryXXX="YY" attributes added to some of my DOM elements
问题描述
当我使用IE开发人员工具时,某些DOM元素添加了以下形式的属性: jQueryXXXXXXXXX ="YY" 其中XXXXX是一个相当长的数字字符串,而YY通常是一个较小的整数值.
When I use the IE developer tools, some of the DOM elements have attributes added that are of the form jQueryXXXXXXXXX="YY" where XXXXX is a fairly long digit string and YY is a usually small integer value.
我在Safari上的DOM检查器中看不到这些.
I don't see these with the DOM inspector on Safari.
为什么以及何时添加这些内容?数据对我有任何帮助吗?
Why and when are these added? Is the data useful to me in any way?
推荐答案
这是jQuery expando属性,它是用于在$.cache
中查找其条目的对象上的键. $.cache
用于 .data()
,事件处理程序或您想要粘贴在其中的任何内容,它是一个集中的事件存储位置(使触发全局事件更容易/更有效率)和一个清理位置.通过仅在元素上携带属性,就不必在每个元素上存储数据,而该数据存储可能无法跨浏览器正确克隆,而是仅维护此键,并可以在$.cache
对象随时出现.
This is the jQuery expando attribute, it's a key on the object used to find it's entry in $.cache
. $.cache
is used for .data()
, event handlers, or anything you want to stick in there, it's a centralized place to store events (makes firing global events easier/more efficient as well) and one place for cleanup. By carrying only the attribute on the element, it's not necessary to have a data store on each element which may not clone correctly-cross browser, rather it only maintains this key, and can lookup it's entry in the $.cache
object at any point.
让我们举个例子:
domElement[$.expando] //only works in 1.4+, expando was private previously
这将给出一个"ID"或各种键,该键对应于$.cache[4].
This will give an "ID" or key of sorts, that key corresponds to the property on the $.cache
object that stores this element's data/events (if it has any data/event handlers). For example if the key was "4", it would be used internally to access $.cache[4]
.
$.cache
包含jQuery分配的所有元素的所有数据,事件处理程序等.通过递增 $.uuid
(内部每当将 new 对象添加到$.cache
中时,不断攀升的ID jquery就会分配并递增.
$.cache
contains all data, event handlers, etc for all elements that were assigned by jQuery. It's assigned by incrementing the $.uuid
(an internal ever climbing ID jquery assigns and increments any time a new object's added into $.cache
).
一些额外的位:
名称的随机性不是全部随机的,jQueryXXXXXXXXXXXXXXXXX
是只需jQuery
+加载jquery的时间戳,即可为该属性赋予唯一的希望不冲突的名称.
The random nature of the name isn't all that random, the jQueryXXXXXXXXXXXXXXXXX
is just jQuery
+ the timestamp then jquery was loaded, to give the attribute a unique hopefully non-colliding name.
为什么不使用.html()
看到它?是因为 regex将其删除.
Why don't you see it with .html()
?, well because jQuery hides it, it does a regex to strip it out.
注意: $.expando
不在1.3中公开,只有1.4 +.
Note: $.expando
isn't exposed in 1.3, only 1.4+.
用法:
有用吗?好吧,例如,如果您在控制台中分析$.cache
,并且看到内存泄漏(没有 .empty()
在许多 .load()
调用之前,例如将事件处理程序留在后面) .打开控制台,然后执行$.cache
,在那里看到500个条目,假设您想知道哪个对象与312一起使用,然后可以选择它,就像这样:
Is it useful? Well it can be, for example if you analyze $.cache
in your console, and you see you have a memory leak (no .empty()
before many .load()
calls, leaving event handlers behind for example). You open your console, and do $.cache
, you see 500 entries there, let's say you want to know which object went with 312, then you can select it, like this:
$("[" + $.expando + "=312]")[0] //DOM element for this entry
再举一个例子,
$("#myElem").data('events') //get events object, equivalent to:
$.cache[$("#myElem")[0][$.expando]].events
这是一个很方便的示例,通常jQuery的普通用户不需要深入了解$.cache
或它是如何工作的,但是它在那里并且可以使用,以防万一您不必去寻找.只需在控制台中运行$.cache
,就可能有大量您不知道的有关所有处理程序的信息:)
This is one example that's handy, typically the average jQuery user does't need to dive into $.cache
or how it works, but it is there and available in case you never need to go looking. Just run $.cache
in your console, there's likely a wealth of information about all your handlers that you didn't know was available :)
这篇关于我看到jQueryXXX ="YY"属性添加到我的某些DOM元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!