javascript - 我怎样才能在这个计数器脚本的 1000 中的数字上添加逗号

标签 javascript jquery number-formatting

所以这个脚本会计算我添加到范围中的数字,但我想在 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/

相关文章:

javascript - 关系和逆关系不能一起工作 Adonis JS

javascript - scriptNode.onaudioprocess 已弃用,还有其他选择吗?

java - 如何在 GUI 中设置货币格式?

java - 关于NumberFormatter的一个问题

excel - 使用 VBA 将单元格格式化为任意货币,无论区域设置如何

javascript - 如何在 JavaScript 中克隆对象数组?

javascript - cakephp 2.x 在 javascript 中执行 Controller

jquery - 在.Net MVC 5的同一页面中创建 "One to many"的 View

SlideDown、SlideUp 上的 jQuery mouseenter、mouseleave 导致问题

javascript - 我可以使用 JS 一次使用 JSON 数组中的一个字符串吗?