jQuery。数据()不检索数据* [英] jQuery .data() not retrieving data-*

查看:107
本文介绍了jQuery。数据()不检索数据*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IE8进行测试。我刚刚将jQuery从v1.5.2升级到了v1.6.1,现在data方法不起作用。



该行如下所示:

 < tr class =ui-widget-content altnodeIndex =2data-DocAttributeFieldType =TextBoxdata-DocClassAttributeFieldId =60777jQuery16106588245076914028 = 66 > 

此作品:

  $(#docClassAttributeFields tbody tr:first)。attr(data-DocClassAttributeFieldId); 

这不起作用:

<$ p $数据(DocClassAttributeFieldId);数据(DocClassAttributeFieldId);数据(DocClassAttributeFieldId);

是否有错误?

这是一个例子。在1.5.2和1.6中运行它,看看它们的行为有何不同...
http://jsfiddle.net/5hbKX/

解决方案

docs (我怀疑1.6中提到的更改是否应归咎于 - 你是否尝试删除该案例,查看lastValue示例?)


$ b


HTML 5数据 - 属性

截至jQuery 1.4.3 < a href =http://ejohn.org/blog/html-5-data-attributes/ =noreferrer> HTML 5数据 -
属性
将被自动取消in to
jQuery的数据对象。使用嵌入破折号处理
属性是在jQuery 1.6中改变
以符合
W3C HTML5
规范



例如,给定以下HTML:

 < div data-role = pagedata-last-value =43data-hidden =truedata-options ='{name:John}'>< / div> 

所有以下jQuery代码都将
工作。

  $(div)。data(role)===page; 
$(div)。data(lastValue)=== 43;
$(div)。data(hidden)=== true;
$(div)。data(options)。name ===John;

每次尝试将
字符串转换为JavaScript值(此
包含布尔值,数字,对象,
数组和空值),否则它将以
作为字符串。要检索值的
属性作为字符串而没有任何
尝试转换它,请使用attr()
方法。当数据属性是
对象(以'{')或数组
开头(以'['开头'),然后
时,jQuery.parseJSON用于解析
字符串;它必须遵循有效的JSON
语法,包括带引号的属性
名称。在数据属性第一次访问
时,数据属性被拉为
,然后不再访问或变异
(所有数据值
随后存储在jQuery内部) 。


来自上面 HTML5规范


$ b


一个自定义数据属性不是名称空间中的
属性,其名称
以字符串 data-,在连字符后至少有一个字符b $ b,
是XML兼容的,并且在U + 0041到
U + 005A(拉丁语)范围内不包含
字符大写字母A到
拉丁大写字母Z)。


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


I'm testing with IE8. I just upgraded jQuery from v1.5.2 to v1.6.1 and now the data method isn't working.

the row look like this:

<tr class="ui-widget-content alt" nodeIndex="2" data-DocAttributeFieldType="TextBox" data-DocClassAttributeFieldId="60777" jQuery16106588245076914028="66">

this works:

$("#docClassAttributeFields tbody tr:first").attr("data-DocClassAttributeFieldId");

this does not work:

$("#docClassAttributeFields tbody tr:first").data("DocClassAttributeFieldId");

Is there a bug in it?

Here is an example. Run it with in 1.5.2 and then 1.6 to see how they act differently... http://jsfiddle.net/5hbKX/

解决方案

From the docs (I suspect the change mentioned in 1.6 is to blame - have you tried removing the case, look at the lastValue example?):

HTML 5 data- Attributes

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.

For example, given the following HTML:

<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>

All of the following jQuery code will work.

$("div").data("role") === "page";
$("div").data("lastValue") === 43;
$("div").data("hidden") === true;
$("div").data("options").name === "John";

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method. When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

From the above HTML5 specification:

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).

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

这篇关于jQuery。数据()不检索数据*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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