在数据属性中区分大小写 [英] Case sensitive in data attribute

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

问题描述

那一定是迟到了,我的大脑麻木了。 jQuery如何在数据属性中不区分大小写?我遇到了这个令人讨厌的问题:

HTML:

 < a data-showId =12345>测试1< / a> 

Javascript:

 的console.log($( 'a')的数据( 'showId')); 
console.log($('a')。data('showid'));

第一行是未定义,第二行返回 12345 正确。我认为它应该在第一行正确返回,在第二行未定义。那么这是否意味着所有的数据都必须小写?



在这里查看 http://jsfiddle.net/qhoc/7dExt/1/

解决方案

我认为这与 data- * 有关明确地防止数据密钥有大写A到Z的字母。


  • 名称不能以xml开头,无论这些字母用什么大小写;

  • 名称不得包含任何分号(U + 003A);

  • 该名称不得包含大写A到Z的字母。



在data- *命名方案中,像 data-show-id 这样的数据属性可以通过使用键 showId 的javascript访问,因为这种使用大写字母的限制似乎受到限制。



阅读:


HTML中HTML元素的所有属性文件自动获得ASCII小写字母
,所以对ASCII大写字母的限制不会影响这些文件


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:

All attributes on HTML elements in HTML documents get ASCII-lowercased automatically, so the restriction on ASCII uppercase letters doesn't affect such documents

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

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