jquery – 数据属性变为整数

前端之家收集整理的这篇文章主要介绍了jquery – 数据属性变为整数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
看看这个简单的例子 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'));

From the 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相关文章