javascript - 如何保持数据值(value)不变?

标签 javascript jquery html custom-data-attribute jquery-data

我有这个基本的 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

Updated Fiddle

但实际上,如果您使用 jQuery 1.8+ 版本应该没问题。

关于javascript - 如何保持数据值(value)不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423275/

相关文章:

javascript - Vue js将所有文本放入json文件并导入

javascript - 无法删除本地存储数据

java - 如何在 GWT 中将 ScrollHandler 绑定(bind)到 body 上?

javascript - 为什么以及何时我们在 node.js 中使用 response.end() 并返回 response.end()?

javascript - Vue.js:导入相同的 css 文件时定义了重复的 CSS 类(CSS 组件)

javascript - 如何在自定义集群标记内打开 InfoWindow - Google map

javascript - 在提交时单击按钮启用下一个选项卡?

javascript - Jquery 将选定的类添加到 li

javascript - 颜色框定位在另一个元素之上

html - 为什么在清除 float 后不应用最高 margin ?