简体   繁体   English

jQuery 2.2.3获取“数据”属性的未定义

[英]jquery 2.2.3 get undefined for “data” attribute

I use jquery 2.2.3 and I have this div: 我使用jQuery 2.2.3,我有这个div:

<div class="typeahead" data-minLength="1" data-queryURL="url"></div>

If I do: 如果我做:

console.log(jQuery('.typeahead').attr('data-queryURL')

I get "url". 我得到“ URL”。

If I do: 如果我做:

console.log(jQuery('.typeahead').data('queryURL')

I get "undefined". 我得到“未定义”。

我认为jquery .data()支持小写键名。

console.log(jQuery('.typeahead').data('queryurl'));

Uppercase characters are not supported in data-* attributes. data- *属性不支持大写字符。 The HTML5 spec says: HTML5规范说:

3.2.3.8 Embedding custom non-visible data with the data-* attributes A custom data attribute is an attribute in no namespace whose name starts with the string "data-", has at least one character after the hyphen, is XML-compatible, and contains no characters in the range U+0041 to U+005A (LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z). 3.2.3.8使用data- *属性嵌入自定义非可见数据自定义数据属性是没有名称空间的属性,该名称空间的名称以字符串“ data-”开头,且在连字符后至少有一个字符,并且与XML兼容,且不包含U + 0041到U + 005A范围内的字符(拉丁大写字母A到拉丁大写字母Z)。

Explained also here . 这里也解释。

This works well: 这很好用:

console.log(jQuery('.typeahead').attr('data-queryurl'));
console.log(jQuery('.typeahead').data('queryurl'));

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

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