哪个更好:数据还是属性? [英] what is better: data or attr?
本文介绍了哪个更好:数据还是属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么更好的?
var itemId=8;
$('#aaa').data('ItemId', itemId);
$('#aaa').attr('ItemId', itemId);
数据还是属性?
推荐答案
它们具有不同的用途.
They serve different purposes.
- 如果要存储与元素关联的常规数据,请使用
.data()
. - 如果要更改DOM元素的属性,请使用
.attr()
.
- If you want to store general data associated with an element, use
.data()
. - If you want to change the attributes of the DOM element, use
.attr()
.
不要发明"新属性(来自 HTML5数据属性的应用).但是当您使用jQuery时,请坚持使用.data()
.
Don't "invent" new attributes (appart from HTML5 data attributes). But as you are using jQuery, stick with .data()
.
这篇关于哪个更好:数据还是属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文