获取数据属性jquery vs javascript [英] Get data-attribute jquery vs javascript
问题描述
我默认设置了自定义数据属性:
I have a custom data-attribute set by default:
data-equipment =0
如果我使用 .data()
$(this).data(equipment,10)
然后使用 getAttribute()
this.getAttribute(data-equipment)
我得到旧值(0)而不是新值(10)。但是,如果我使用
i get the old value (0) and not the new one (10). But if i use
$(this).data(equipment)
我得到新值(10 )。
$(this).data("equipment")
i get the new value (10).
这应该是这样的吗?还是我错过了什么?
Is this supposed to work like this or am i missing something?
谢谢!
推荐答案
.data ()
不对数据属性进行操作,而是对内部jQuery缓存进行操作。最初如果没有找到缓存记录,则从相应的 data -
属性中读取数据(如果存在),但这是他们合作的结束。
.data()
doesn't operate on data attributes but in internal jQuery cache. Initially if no cache record is found, the data is read from a corresponding data-
attribute if one exists, but that is the end of their co-operation.
如果它对属性进行操作,那么它的用途就没用了,因为属性值必须是字符串。
If it operated on attributes, it would be useless for its purpose because attribute values must be strings.
这篇关于获取数据属性jquery vs javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!