有没有办法阻止.data()函数将数据转换为另一种类型?
例如,使用HTML< div data-code-name =“007”> Bond,James< / div>
$(“div”).data(“codeName”)返回7而不是“007”
示例:http://jsfiddle.net/dMHS4/
更新:我刚刚注意到,如果你改为jQuery 1.8.2,它就不会转换. http://jsfiddle.net/dMHS4/2/
更新:Has jQuery 1.8 changed how .data() returns the value?
解决方法
使用.attr()而不是.data():
$("div").attr("data-code-name")
http://jsfiddle.net/dMHS4/1/
取自jQuery documentation:
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