我有这个基本的 HTML:
<div id ="div1" data-pagenumber="0001">First page</div>
<div id = "div2"></div>
和我的 jQuery:
var number = $('#div1').data('pagenumber')
$('#div2').append(number);
fiddle :http://jsfiddle.net/JabUS/
为什么div2
里面的文字设置为1
?在我看来,jQuery 自动转换了我的值。我如何阻止这种转换,以便它像这样打印,0001
?
最佳答案
你需要使用:
var number = $('#div1').attr('data-pagenumber');
因为来自 jQuery docs :
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
但实际上,如果您使用 jQuery 1.8+ 版本应该没问题。
关于javascript - 如何保持数据值(value)不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423275/