Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向jQuery计数器添加千位分隔符_Javascript_Jquery_Formatting - Fatal编程技术网

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%的类似问题答案所做的。