看看这个简单的例子
on jsfiddle
<div id ="a" data-siteid="00005">00005 turns into:</div> <div id="b" data-siteid="S00005">S00005 turns into: </div>
码
$('#a').append($('#a').data("siteid")); $('#b').append($('#b').data("siteid"));
结果
00005 turns into:5 S00005 turns into: S00005
我想返回“00005”和“S00005”.
解决方法
尝试
$('#a').append($('#a').attr('data-siteid')); $('#b').append($('#b').attr('data-siteid'));
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.