jQuery是否内部将HTML5数据属性键转换为小写? [英] Does jQuery internally convert HTML5 data attribute keys to lowercase?

查看:93
本文介绍了jQuery是否内部将HTML5数据属性键转换为小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图尽可能使我的JavaScript编码风格符合我的Zend编码风格,这就是使用camelCase。所以,在我的HTML5数据属性中,我在这个例子中命名它们:

 < button class =actiondata -actionClass =userdata-actionMethod =deletedata-actionRequest =/ user / delete / user-id / 1data-actionComplete ={reload:users}>删除用户#1< / button> 
< div id =usersdata-reloadRequest =/ user / index> ...< / div>

利用Jquery进行操作的非常不引人注目的方式,但是当我调用$('。action')时。 data(),属性名称被转换为小写。



这有什么变通方法吗?



我从来没有JavaScript变量应该有破折号,我不明白为什么jQuery内部为我做这个?或者它可能是HTML5?

解决方案

如果您使用

<$ p $然后你就可以访问数据操作方法=删除

属性

  $('。action')。data('actionMethod')

这是 HTML5 DOM API
$ b


自定义数据属性转换为具有以下规则的
DOMStringMap条目:


  • 任何破折号(U + 002D)被删除;
  • 破折号后面的任何字母(U + 002D),在删除之前,将其设置为其大写对应字母。


I am trying to conform my JavaScript coding style to my Zend coding style as much as possible, which is using camelCase. So, in my HTML5 data attributes, I am naming them as in this example:

<button class="action" data-actionClass="user" data-actionMethod="delete" data-actionRequest="/user/delete/user-id/1" data-actionComplete="{reload:users}">Delete User #1</button>
<div id="users" data-reloadRequest="/user/index"> ... </div>

Pretty unobtrusive way to harness Jquery for actions, but when I call $('.action').data(), the attribute names are converted to lowercase.

Any workarounds for this?

I never though JavaScript variables should have dashes in them, and I can't understand why jQuery is internally doing this for me? Or maybe it is HTML5?

解决方案

If you use

data-action-method="delete"

then you can access the attribute with

$('.action').data('actionMethod')

This is part of the HTML5 DOM API:

The custom data attributes is transformed to a key for the DOMStringMap entry with the following rules:

  • any dash (U+002D) is removed;
  • any letter following a dash (U+002D), before its removal, is set in its uppercase counterpart.

这篇关于jQuery是否内部将HTML5数据属性键转换为小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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