数据属性变为整数 [英] Data attribute becomes integer

查看:71
本文介绍了数据属性变为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看这个简单示例在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".

I would like to return "00005" and "S00005".

推荐答案

尝试

$('#a').append($('#a').attr('data-siteid'));
$('#b').append($('#b').attr('data-siteid'));

来自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.

这篇关于数据属性变为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆