简体   繁体   English

jQuery是否在内部将HTML5数据属性键转换为小写?

[英]Does jQuery internally convert HTML5 data attribute keys to lowercase?

I am trying to conform my JavaScript coding style to my Zend coding style as much as possible, which is using camelCase. 我试图尽可能地将我的JavaScript编码风格与我的Zend编码风格相符合,这是使用camelCase。 So, in my HTML5 data attributes, I am naming them as in this example: 因此,在我的HTML5数据属性中,我将其命名为以下示例:

<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. 将Jquery用于操作的非常不引人注目的方式,但是当我调用$('。action')。data()时,属性名称将转换为小写。

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? 我从不认为JavaScript变量应该包含破折号,我无法理解为什么jQuery在内部为我做这个? Or maybe it is HTML5? 或许它是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 : 这是HTML5 DOM API的一部分

The custom data attributes is transformed to a key for the DOMStringMap entry with the following rules: 自定义数据属性将转换为DOMStringMap条目的键,并具有以下规则:

  • any dash (U+002D) is removed; 删除任何破折号(U + 002D);
  • any letter following a dash (U+002D), before its removal, is set in its uppercase counterpart. 短划线(U + 002D)之后的任何字母,在删除之前,都以其大写对应方式设置。

First off, see this part of the source code of JQuery, it assumes you have lower case attributes. 首先,看看这部分 JQuery的源代码,它假设你有小写属性。

Secondly, by convention, all HTML5 attributes should be lowercase, see: http://www.htmlbasictutor.ca/html-tag-attributes.htm 其次,按照惯例,所有HTML5属性都应该是小写的,请参阅: http//www.htmlbasictutor.ca/html-tag-attributes.htm

Finally, be warned you may encounter futher problems if you insist on using upper cases, see Django: Unable to add UPPERCASE attribute name in HTML input element 最后,请注意,如果您坚持使用大写,可能会遇到更多问题,请参阅Django:无法在HTML输入元素中添加UPPERCASE属性名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM