jQuery向节点添加一个奇怪的属性 [英] jQuery is adding a strange attribute to nodes

查看:115
本文介绍了jQuery向节点添加一个奇怪的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用IE8和jQuery 1.4.2。我的网页不再正确渲染,并且在HTML中的调试器中快速查看,现在每个元素都有一个名为jQuery1279875396122的新属性,它的值是一个小整数,显然对每个节点都是唯一的。



查看jQuery源代码,我可以看到长数字来自(new Date).getTime(),但这是我理解的极限,没有太多的研究。



我不知道这是否与我的渲染问题有关,但我以前从未注意到它,在IE8或任何其他浏览器中。有人可以解释这些属性是什么吗?解决方案jQuery使用这些expando属性来跟踪与元素相关的数据。 jQuery使用它的数据API进行事件处理,加上你可能想绑定到元素的任何通用数据(使用 $。data )。



属性( jQuery1279875396122 )将具有与 jQuery.cache 中的某个位置相关的值。



jQuery没有将数据直接保存到元素(作为常规属性)的原因是为了避免内存泄漏,并且通常不那么突兀。






就像一个例子,当你将一个事件处理程序绑定到一个元素时,就像这样:

  jQuery('div')。click(doSomething); 

doSomething 函数将存储在 jQuery.cache ,并且在其基本级别上,它的位置(或者说,引用它的对象的位置)将被分配给元素的 jQuery1279875396122 属性。 jQuery仍将使用浏览器的本地API绑定到元素的事件,但当它被触发时,jQuery将查找(在 jQuery.cache 中)并调用正确的处理程序。



编辑:为了清楚起见,这些属性并不值得关注。您应该期望在所有通过jQuery绑定数据的元素上看到它们(包括事件处理程序)。如果这是您渲染问题的原因,我会非常惊讶。


I'm using IE8 and jQuery 1.4.2. My web page is no longer rendering correctly, and a quick look in the debugger at the HTML shows that every element now has a new attribute called "jQuery1279875396122", whose value is a small integer, apparently unique to each node.

Looking at the jQuery source I can see that the long number comes from (new Date).getTime(), but that's the limit of my understanding without a lot more research.

I don't know if this is related to my rendering problem, but I've never noticed it before, in IE8 or any other browser. Can someone explain what these attributes are?

解决方案

jQuery uses these "expando" properties to keep track of data associated with elements. jQuery uses its data API for event handling plus any general data that you may want bound to an element (using $.data).

The property (jQuery1279875396122) will have a value associated with a position in jQuery.cache.

The reason jQuery doesn't save data directly to the element (as regular properties) is to avoid memory leaks and just to generally be a little less obtrusive.


Just as an example, when you bind an event handler to an element, like so:

jQuery('div').click(doSomething);

The doSomething function will be stored in jQuery.cache and, on a rudimentary level, its position (or rather, the position of the object that references it) will be assigned to the element's jQuery1279875396122 property. jQuery will still use the browser's native API to bind to the element's event but when it's fired jQuery will lookup (in jQuery.cache) and call the correct handlers.

EDIT: Just to be clear, these properties are not a cause for concern. You should expect to see them on all elements that have any data bound via jQuery (including event handlers). I would be very surprised if this was the cause of your rendering problem.

这篇关于jQuery向节点添加一个奇怪的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆