Jquery data() 存储
谁能告诉我 jquery data()
在哪里存储数据以及何时删除以及如何删除?
如果我用它来存储ajax调用结果,会有性能问题吗?
例如:
$("body").data("test", { myData: 'abcd'});
Can any one tell me where jquery data()
stores the data and when it is get erased and how?
Is there any performance issue if I use this to store ajax call result?
For example:
$("body").data("test", { myData: 'abcd'});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有数据都存储在名为
cache
的jQuery
对象的属性内。在控制台中记录$.cache
的内容,以查看与任何 DOM 元素关联的所有数据和事件。jQuery 将 DOM 对象与此缓存中的对象链接起来的方式是通过操作 DOM 对象。假设我们有一个输入元素
,其数据键名为“foo”,
现在 jQuery 维护一个
jQuery
形式的随机字符串,例如jQuery1291790929680
,也可以通过 $.expando 访问。 jQuery 将此 Expando 字符串添加为具有关联数据项或事件的每个 DOM 对象的键。因此,上述输入元素的 DOM 对象将包含带有某个整数值的扩展键,例如:4 只是一个随机示例,但该数字表示
$.cache
对象中的索引,其中存储该 DOM 对象的关联数据和事件。因此,有了这些信息,要检索上述输入元素的数据,我们可以间接编写:应该返回“bar”,这是一种间接编写方式
$(e).data("foo").
上述废话的图解示例:)
All data is stored inside a property of the
jQuery
object namedcache
. Log the contents of$.cache
in your console to see all data and events associated with any DOM element.The way jQuery links up a DOM object with an object in this cache is by manipulating the DOM object. Say we have a input element
which has a data key named "foo"
Now jQuery maintains a random string of the form
jQuery<current time in ms>
, for example,jQuery1291790929680
, which is also accessible by$.expando
. jQuery adds this expando string as a key to each DOM object that has an associated data item or event. So the DOM object for the above input element will contain this expando key with some integer value such as:4 is just a random example, but this number denotes an index in the
$.cache
object, where the associated data and events for this DOM object are stored. So given this information, to retrieve the data of the above input element, we can indirectly write:which should return "bar", which is an indirect way of writing
$(e).data("foo")
.An illustrated example of the above nonsense :)
查看来自 jquery 的内容
jQuery.data() 方法允许我们以一种安全的方式将任何类型的数据附加到 DOM 元素,从而避免循环引用,从而避免内存泄漏。 jQuery 确保通过 jQuery 方法删除 DOM 元素以及用户离开页面时删除数据。我们可以为单个元素设置多个不同的值并稍后检索它们:
see the content from jquery
The jQuery.data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore free from memory leaks. jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page. We can set several distinct values for a single element and retrieve them later: