在jQuery Counter中添加千位分隔符 [英] Adding thousand separator to jQuery Counter

查看:188
本文介绍了在jQuery Counter中添加千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to add commas (thousand separator) for big numbers that use the follow counter function:

我试图为大数字添加逗号(千分号)类型= '文本/ JavaScript的' >
$(window).load(function(){
$('。Count')。each(function(){
var $ this = $(this);
({Counter:0})。animate({Counter:$ this.text()},{
duration:1500,
easing:'swing',
step:function() {
$ this.text(Math.ceil(this.Counter));
}
});
});
});

<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)); } }); }); });

我是否修改这个特定的公式或者是否必须编写一个额外的函数来处理格式?

Do I modify this particular formula or do I have to write an additional function that will handle the formatting?

推荐答案



this might work

<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>

这篇关于在jQuery Counter中添加千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆