jQuery属性自动添加到元素

jQuery属性自动添加到元素

本文介绍了jQuery属性自动添加到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用开发人员工具中内置的IE8后,我注意到jQuery将属性附加到一些元素上:替代文本http://thebe.jtan.com/~vince/stack/jquery.PNG

After using the IE8 built in Developers Tools for the first time, I noticed jQuery is attaching an attribute to some of my elements:alt text http://thebe.jtan.com/~vince/stack/jquery.PNG

我以前从未注意到这一点.实际上,这在Firebug中不会出现...我现在只是在IE8开发人员工具中第一次看到它.有谁知道jQuery将其用于什么以及为什么将其隐藏在Firebug中?

I've never noticed this before. In fact, this doesn't show up in Firebug... I'm only seeing it for the first time now in IE8 Developer Tools. Does anyone know what jQuery uses this for, and why it's hidden in firebug?

推荐答案

jQuery源是非常容易阅读,您可以查看data函数的作用.

The jQuery source is pretty easy to read, and you can look at what the data function is doing.

总结:

  • jQuery有一个名为"expando"的变量,其为'jQuery'+(+new Date)
  • jQuery有另一个名为uuid的变量,它以"1"开头
  • jQuery.cache是一个空对象
  • 在HTML元素/对象上设置/获取任何数据"将使用对象上的expando属性将引用存储到jQuery.cache中-类似于:

  • jQuery has a variable called 'expando' which is 'jQuery'+(+new Date)
  • jQuery has another variable called uuid which starts with "1"
  • jQuery.cache is an empty object
  • Setting/Getting any "data" on a HTML Element/Object will use the expando property on the object to store a reference into jQuery.cache -- sort of like this:


// get the elements cache id, or create a new cache id:
var id = elem[expando] || (elem[expando] = uuid++);
// get the cache for the element, or create it:
var data = jQuery.cache[id] || (jQuery.cache[id] = {});

事件处理程序存储在此内部data对象的eventshandle属性中.

Event Handlers are stored in the events and handle properties of this internal data object.

因此,内部使用 .data() 分配的所有属性都在HTML将密钥存储到jQuery的内部数据缓存中. jQuery事件处理程序也存储在同一缓存中.分配给expando的数值是一个递增计数器,该引用引用其在jQuery缓存对象中的位置.

So, internally all properties that are assigned using .data() use this "expando" attribute on the HTML to store a key into jQuery's internal data cache. jQuery event handlers are also stored in this same cache. The numeric value assigned to the expando is an incrementing counter that references its location in the jQuery cache object.

这篇关于jQuery属性自动添加到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 17:04