我有代码jquery但我希望每秒加值0.00001
$(document).ready(function(){
setInterval(function()
{
var id = $('.id').attr('rel');
var st = (parseFloat(id) + parseFloat('0.00001')).toFixed(1);
$('.id').html(st);
$('.id').attr('rel',st);
}, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
// html output
<span class='id' rel='0.00001'>0.00001</span>
答案 0 :(得分:1)
问题是因为您使用toFixed(1)
四舍五入到小数点后1位。将其更改为toFixed(5)
,您的代码运行正常。虽然注意将字符串文字转换为浮点数有点多余,但只需在附加中直接使用float:
$(document).ready(function() {
setInterval(function() {
var id = $('.id').attr('rel');
var st = (parseFloat(id) + 0.00001).toFixed(5);
$('.id').html(st).attr('rel', st);
}, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
// html output
<span class="id" rel="0.00001">0.00001</span>