如何保留数据属性的大小写? [英] How to preserve the case of a data attribute?

查看:106
本文介绍了如何保留数据属性的大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

html对象:

 <div data-myAttribute="test"></div>

代码:

 var $o = $("div");

 $.each($o.data(),function(k,v){
    console.log(k); 
    //writes 'myattribute' instead of 'myAttribute'
 });

如何保留属性的大小写?

How do I preserve the case of the attribute?

推荐答案

如果您的目标是将 myAttribute 定位为数据集的键 property,你应该使用 data-my-attribute

If your goal is to target myAttribute as key of dataset property, you should use data-my-attribute:

<div data-my-attribute="test"></div>

请参阅以下有关camelCased规则的链接:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset

See following link regarding camelCased rule: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset

PS:as Izkata 评论它:

PS: as Izkata commented it:


供其他人参考,jquery也是进行转换,使
$ div.data('my-attribute')返回与
$ div.data('myAttribute')相同的内容。 vanilla javascript数据集属性
不会这样做。

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.

这篇关于如何保留数据属性的大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆