我如何在这个计数器脚本的 1000 中为数字添加逗号

how can i add a comma to a number in it's 1000 to this counter script

所以这个脚本计数到我添加到跨度的数字,但我想在 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>

使用这个功能:

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, ",") ); 
    })
}

然后:

$(".count counter").digits();

您也可以使用这个简单的代码来做同样的事情:

$(this).text(now.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, ","));