html对象:
<div data-myAttribute="test"></div>@H_301_4@代码:
var $o = $("div"); $.each($o.data(),function(k,v){ console.log(k); //writes 'myattribute' instead of 'myAttribute' });@H_301_4@如何保留属性的大小写?
解决方法
如果您的目标是将myAttribute作为数据集属性的关键字,则应使用data-my-attribute:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset @H_301_4@PS:as Izkata评论:
<div data-my-attribute="test"></div>@H_301_4@有关camelCased规则,请参阅以下链接:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset @H_301_4@PS:as Izkata评论:
@H_301_4@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.