停止jQuery .data()的转换 [英] Stop jQuery .data() from converting
问题描述
是否有一种方法可以阻止.data()函数将数据转换为另一种类型?
Is there a way to stop the .data() function from converting the data to another type?
例如,使用HTML <div data-code-name="007">Bond, James</div>
For example, with the HTML <div data-code-name="007">Bond, James</div>
$("div").data("codeName")
返回7,而不是"007"
$("div").data("codeName")
returns 7 instead of "007"
示例: http://jsfiddle.net/dMHS4/
更新:我只是在小提琴中注意到,如果您更改为jQuery 1.8.2,则不会进行转换. http://jsfiddle.net/dMHS4/2/
Update: I just noticed in the fiddle that if you change to jQuery 1.8.2 it does not convert. http://jsfiddle.net/dMHS4/2/
更新: jQuery 1.8是否更改了.data()返回值的方式?
推荐答案
使用.attr()
代替.data()
:
$("div").attr("data-code-name")
来自 jQuery文档:
每次都尝试将字符串转换为JavaScript值 (这包括布尔值,数字,对象,数组和null),否则 它以字符串形式保留.以字符串形式检索值的属性 无需尝试对其进行任何转换,请使用attr()方法
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 .data()的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!