Javascript 向jQuery计数器添加千位分隔符
我正在尝试为使用以下计数器功能的大数字添加逗号千位分隔符:Javascript 向jQuery计数器添加千位分隔符,javascript,jquery,formatting,Javascript,Jquery,Formatting,我正在尝试为使用以下计数器功能的大数字添加逗号千位分隔符: <script type='text/javascript'> $(window).load(function() { $('.Count').each(function() { var $this = $(this); jQuery({Counter: 0}).animate({Counter: $this.text()}, {
<script type='text/javascript'>
$(window).load(function() {
$('.Count').each(function() {
var $this = $(this);
jQuery({Counter: 0}).animate({Counter: $this.text()}, {
duration: 1500,
easing: 'swing',
step: function() {
$this.text(Math.ceil(this.Counter));
}
});
});
});
我是否要修改这个特定的公式,还是必须编写一个额外的函数来处理格式设置?这可能行得通
<script>
$(window).load(function() {
$('.Count').each(function() {
var $this = $(this);
jQuery({Counter: 0}).animate({Counter: $this.text()}, {
duration: 1500,
easing: 'swing',
step: function() {
var num = Math.ceil(this.Counter).toString();
if(Number(num) > 999){
while (/(\d+)(\d{3})/.test(num)) {
num = num.replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
}
}
$this.text(num);
}
});
});
});
</script>
我也有同样的问题,大量的数字结尾不一致,即550000有时只算549826左右 下面是一个简化的解决方案,它允许LocaleString函数相应地添加小数: $'.js count'.eachfunction{ $this.prop'Counter',0.0动画{ 计数器:$this.data'number' }, { 持续时间:4000, 放松:"摇摆",, 步骤:现在开始{ $this.textMath.ceilnow.toLocaleString'en'; } }; };
那么,你想要解决的问题是什么呢?我正试图找到一种方法,让我的计数器动画在大量使用逗号的数字上运行。效果非常好!也许你能帮我解决另一个问题。我有大的和小的数字使用动画。小的客场得分正确,但大的客场得分几乎总是相差几分。知道为什么会这样吗?嗯,不,我不知道你的意思。有什么我可以看的吗?登录只是为了提高投票率,toLocaleString方法比使用正则表达式更简单、更干净,这是95%的类似问题答案所做的。