我使用jquery 2.2.3,我有这个div:
<div class="typeahead" data-minLength="1" data-queryURL="url"></div>
如果我做:
console.log(jQuery('.typeahead').attr('data-queryURL')
我得到“网址”。
如果我做:
console.log(jQuery('.typeahead').data('queryURL')
我得到“未定义”。
data- *属性不支持大写字符。 HTML5规范说:
3.2.3.8使用data- *属性嵌入自定义不可见数据自定义数据属性是no namespace中的一个属性,其名称以字符串“data-”开头,在连字符后至少有一个字符,与XML兼容,并且不包含U + 0041到U + 005A范围内的字符(LATIN CAPITAL LETTER A到LATIN CAPITAL LETTER Z)。
还解释了here。
这很好用:
console.log(jQuery('.typeahead').attr('data-queryurl'));
console.log(jQuery('.typeahead').data('queryurl'));
我认为jquery .data()
支持小写的键名。
console.log(jQuery('.typeahead').data('queryurl'));