jQuery data()与attr(data) [英] jQuery data() vs attr(data)
问题描述
我想知道使用$(element).data({'foo' : 'bar'})
与$(element).attr({ 'data-foo' : 'bar' })
将数据保存到元素之间的区别.在将大量数据保存到元素中时,哪种方法最适合使用这两种方法?谢谢
I would like to know the difference between saving data to an element using $(element).data({'foo' : 'bar'})
vs $(element).attr({ 'data-foo' : 'bar' })
. And which of the two method would be best to use when saving a large amount of data in to an element? Thanks
推荐答案
$.data
用于有关元素的 存储 信息:
The $.data
is used for storing information with respect to an element:
存储与指定元素关联的任意数据.退货 设置的值.
Store arbitrary data associated with the specified element. Returns the value that was set.
另一方面,attr
用于操纵元素的 属性 .
On the other hand, attr
is used to manipulate attributes of an element.
从您的问题来看,您似乎要存储数据,在这种情况下应该使用$.data
.
From your question, you seem to store the data, you should use $.data
in that case.
data- *属性是HTML5的功能
.data()似乎对性能更友好. >
.data() seems to be much more performance friendly according to this
我也发现它更干净,因为它在页面源中并不对所有人可见.
I also find it cleaner since it's not visible for everyone in page source.
这篇关于jQuery data()与attr(data)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!