所以这个脚本会计算我添加到范围中的数字,但我想在 11,000 后面添加一个逗号。
如果尝试添加toFixed
但这没有帮助:
$(this).text(Math.ceil(now).toFixed(3));
$('.counter').each(function() {
$(this).prop('Counter', 0).animate({
Counter: $(this).text()
}, {
duration: 7000,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now));
stop();
$(this).removeClass('counter');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="number">
<span class="count counter">100</span>
</div>
<div class="number">
<span class="count counter">11000</span>
</div>
最佳答案
您可以使用toLocalString()
:
$('.counter').each(function() {
$(this).prop('Counter', 0).animate({
Counter: $(this).text()
}, {
duration: 7000,
easing: 'swing',
step: function(now) {
$(this).text(Math.ceil(now).toLocaleString());
// Here --------------------^
stop();
$(this).removeClass('counter');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="number">
<span class="count counter">100</span>
</div>
<div class="number">
<span class="count counter">11000</span>
</div>
关于javascript - 我怎样才能在这个计数器脚本的 1000 中的数字上添加逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447477/