繁体   English   中英

如何在以。(点)或,(逗号)作为千位分隔符的数字上使用jquery函数

[英]How to use jquery function on numbers separated by .(dot) or ,(comma) as thousands separator

我有一个特定的数字,并且使用计数器jquery函数,以便该数字将在特定的时间段内从0变为该数字的动画。 现在,问题是当我以千位分隔符将。(逗号)或。(点)添加到数字时,它不起作用。 我如何使脚本与,(逗号)和。(点)分隔符一起使用,并且也将结果与((逗号)和。(dot)一起返回?

html

<div class='number_container'>
    <span class='count'>317249</span>
</div>

的CSS

.number_container {
    background: red;
    width: 100px;
    text-align: center;
    padding: 38px 0;
    border-radius: 50px;
    color: white;
    margin: 0 auto;
}

.count {
    font: 20px arial;
}

s

$(document).ready(function() {
    $('.count').each(function() {
        $(this).prop('Counter',0).animate({
            Counter: $(this).text()
        }, {
            duration: 3500,
            easing: 'swing',
            step: function(now) {
                $(this).text(Math.ceil(now));
            }
        });
    });
});

Jsfiddle预览

https://jsfiddle.net/y6zdmaeq/

我希望数字317249显示为317,249或317.249,并从0开始计数。谢谢

我已经为您的要求编码了html。 请将其另存为html文件,然后在浏览器中将其打开。 如果您愿意,请投票。 请注意,我已经为它提供了一个外部插件。“编码愉快!!”

 <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript" src="http://www.bendewey.com/code/formatcurrency/jquery.formatCurrency-1.4.0.js"></script> </head> <script> $(document).ready(function() { $('.count').each(function() { $(this).prop('Counter', 0).animate({ Counter: $(this).text() }, { duration: 3500, easing: 'swing', step: function(now) { $("#hiddenInput").val(Math.ceil(now).toString()); var formattedText = $(".currency").formatCurrency().val(); //alert(formattedText); $(this).text(formattedText); } }); }); }); </script> <style> .number_container { background: red; width: 100px; text-align: center; padding: 38px 0; border-radius: 50px; color: white; margin: 0 auto; } .count { font: 20px arial; } </style> <body> <div class='number_container'> <span class='count '>317249</span> <input type="hidden" class="currency" id="hiddenInput"></input> </div> </body> </html> 

结合此答案和您的代码,只需在.text()之前格式化代码

https://jsfiddle.net/y6zdmaeq/2/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM