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

Aar*_*ron 0 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');
    }
  });
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

Mur*_*nik 5

你可以使用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');
    }
  });
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)