html对象:
<div data-myAttribute="test"></div>
代码:
var $o = $("div"); $.each($o.data(),function(k,v){ console.log(k); //writes 'myattribute' instead of 'myAttribute' });
如何保留属性的大小写?
解决方法
如果您的目标是将myAttribute作为数据集属性的关键字,则应使用data-my-attribute:
<div data-my-attribute="test"></div>
有关camelCased规则,请参阅以下链接:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset
For reference for others,jquery also does conversion such that $div.data(‘my-attribute’) returns the same thing as $div.data(‘myAttribute’). The vanilla javascript dataset property does not do this.