【发布时间】:2016-07-10 15:27:05
【问题描述】:
是的,有类似的问题,但它们是关于 jquery 添加小写属性的,如下所示:Does the attr() in jQuery force lowercase?
但我有不同的情况。这是我的 HTML5 代码:
<tr class='projectRow' data-projectId='34'>
注意这是驼峰式。现在这段代码不起作用:
//"this" points to a child element
$id = $(this).closest('.projectRow').data('projectId');//undefined
但是如果我把它变成小写:
$(this).closest('.projectRow').data('projectid');
有效。
当我查看源代码时,它显然是“projectId”(camelCase),但是当在 chrome -> dev tools -> elements 中时,它是“projectid”(小写)o_O
难怪jquery不能得到这个值,但是为什么Chrome会这样做呢?我之前做过数百次类似的事情,虽然在“project-id”中使用了-like,现在经过这么多年制作Web应用程序后,我发现了类似的东西o_O
【问题讨论】:
-
data属性(连同最佳实践中的所有属性名称)应该小写,否则会干扰 jQuery 将它们存储在缓存中的方式。 -
好的,但是为什么 Chrome 会这样做呢?
-
@RoryMcCrossan 如果您有兴趣,这不仅是 jquery 的问题,实际上我正在破坏规范:w3.org/TR/html5/…*-attributes
-
谢谢,我知道 jQuery 不喜欢大小写混合的数据属性,但没有意识到它们在规范中是小写的。
标签: javascript jquery html