Why does jQuery.data () change the value of the HTML5 data-x attribute?

I just met some very disturbing behavior in jQuery 1.6.2, which I hope someone can explain. Given the following markup ...

<div id="test" data-test="    01">Test</div>

Can someone tell me why accessing an attribute through .data()causes it to deal with int?

var t = $("#test").data("test");
alert(t);  // shows "1"

t = $("#test").attr("data-test");
alert(t);  // shows "    01"

Of course, I have proof in jsFiddle of this behavior.

+5
source share
3 answers

From the document for data(key):

Each attempt is made to convert a string to a JavaScript value (this includes logical values, numbers, objects, arrays, and null); otherwise, it remains as a string.

CAN , .

: , , . (.. data(key,value) ), . DOM .

+7

jQuery :

jQuery 1.4.3 HTML- jQuery. jQuery 1.6, W3C HTML5.

(html5 ) JavaScript ( , , , ), . value - , attr().

: http://api.jquery.com/data/

0

,

, .attr().

var t = $("#test").attr("data-test");

: , jQuery, .

http://jsfiddle.net/FzmWa/1/

0

All Articles