<script>
var intVar = parseInt($(.span).data('1'));
alert(data) // output Nan
</script>
---------------
<span id ="span" class ="span" data-1="4" />
我希望'intVar'为整数,但无法解析'data-1'
答案 0 :(得分:2)
适合我(fiddle)......只要你
1:不要提醒data
(因为你没有设置它)并确保你的选择器是一个字符串......
alert(parseInt($('.span').data('1')));
2:将您的脚本放在您尝试访问的dom节点之后:
<span id="span" class="span" data-1="4" />
<script type="text/javascript">
alert(parseInt($('.span').data('1')));
</script>
3。或者像这样包装你的代码(fiddle和reference):
...
<script type="text/javascript">
$(function() {
alert(parseInt($('.span').data('1')));
});
</script>
</head>
<body>
<span id="span" class="span" data-1="4" />
...
答案 1 :(得分:1)
试试这个
var intVar = parseInt($('.span').data('1'));
alert(intVar);
答案 2 :(得分:0)
do this
<script>
$(document).ready(function(){
var intVar = parseInt($('.span').data('1'));
alert(intVar);
});
</script>
<span id ="span" class ="span" data-1="4" />