问题描述
那一定是迟到了,我的大脑麻木了。 jQuery如何在数据属性中不区分大小写?我遇到了这个令人讨厌的问题: HTML:
< a data-showId =12345>测试1< / a>
Javascript:
的console.log($( 'a')的数据( 'showId'));
console.log($('a')。data('showid'));
第一行是未定义,第二行返回 12345 正确。我认为它应该在第一行正确返回,在第二行未定义。那么这是否意味着所有的数据都必须小写?
在这里查看
我认为这与有关明确地防止数据密钥有大写A到Z的字母。
- 名称不能以xml开头,无论这些字母用什么大小写;
- 名称不得包含任何分号(U + 003A);
- 该名称不得包含大写A到Z的字母。
在data- *命名方案中,像 data-show-id
这样的数据属性可以通过使用键 showId
的javascript访问,因为这种使用大写字母的限制似乎受到限制。
阅读:
Well it must be late and my brain got numb. How come jQuery doesn't recognize case sensitive in data attribute? I faced this annoying problem:
HTML:
<a data-showId="12345">Test 1</a>
Javascript:
console.log($('a').data('showId'));
console.log($('a').data('showid'));
The first line is undefined and second returned 12345 correctly. I thought it supposed to returned correctly in first line and undefined in second. So does it mean all data- attr must be lowercase?
Check it out here http://jsfiddle.net/qhoc/7dExt/1/
I think it has to do with the fact that data-* explicitly prevents the data key from having capital A to Z letters.
- the name must not start with xml, whatever case is used for these letters;
- the name must not contain any semicolon (U+003A);
- the name must not contain capital A to Z letters.
In the data-* naming scheme, a data attribute like data-show-id
will be accessible in javascript using the key showId
, because of this limitation the use of capital letters seems to be restricted in the key.
Also read:
这篇关于在数据属性中区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!