jquery – 如何保存数据属性的情况?

前端之家收集整理的这篇文章主要介绍了jquery – 如何保存数据属性的情况?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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:
<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.

原文链接:https://www.f2er.com/jquery/182977.html

猜你在找的jQuery相关文章