jQuery data函数的坑


<div id="test" data-id="1e3"></div>
// 1e3  jQuery 1.9.1
// 1000  jQuery 1.7.2

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn"t change the value"s representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.

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. If the value isn"t parseable as a JavaScript value, 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.

2014-07-16 23:02 更新



我这测试,1.7.2 的是会主动转换的,1.9.1 是不会主动转换