在javascript jquery中显示逗号

在javascript jquery中显示逗号,javascript,jquery,Javascript,Jquery,我正在尝试在网页上显示我的统计数据的一些设置值 javascript是: <script src="****/js/waypoints.min.js" type="text/javascript"></script> <script> $.fn.waypoint.defaults = { context: window, continuous: false, enabled: true, horizontal: fa

我正在尝试在网页上显示我的统计数据的一些设置值

javascript是:

<script src="****/js/waypoints.min.js" type="text/javascript"></script>
<script>
  $.fn.waypoint.defaults = {
     context: window,
     continuous: false,
     enabled: true,
     horizontal: false,
     offset: 0,
     triggerOnce: true
  }

$('.facts_waypoint').waypoint(function(direction) {  
function count($this){
    var current = parseInt($this.html(), 10);
    if (current >= 1000000) {current = current +493023;}
    else if (current >= 10000) {current = current +43749;}
    else if (current >= 1000) { current = current + 7369;}
    else { if (current >= 100) { current = current + 197;}
            else {current = current + 1}
    }
    /*current = current + 100;  Where 1 is increment */

    $this.html(++current);
    if(current > $this.data('count')){
        $this.html($this.data('count'));
    } else {
        setTimeout(function(){count($this)}, 50);
    }
}
jQuery(".count_one, .count_two, .count_three").each(function() {
  jQuery(this).data('count', parseInt(jQuery(this).html(), 10));
  jQuery(this).html('0');
  count(jQuery(this));
});
});

$.fn.waypoint.defaults={
上下文:窗口,
连续:假,
启用:对,
水平:错,
偏移量:0,
触发:对
}
$('.facts_航路点')。航路点(函数(方向){
函数计数($this){
var current=parseInt($this.html(),10);
如果(当前>=1000000){current=current+493023;}
如果(当前>=10000){current=current+43749;}
如果(当前>=1000){current=current+7369;}
else{if(current>=100){current=current+197;}
else{current=current+1}
}
/*电流=电流+100;其中1为增量*/
$this.html(++当前版本);
如果(当前>$this.data('count')){
$this.html($this.data('count'));
}否则{
setTimeout(函数(){count($this)},50);
}
}
jQuery(“.count\u one、.count\u two、.count\u three”)。每个(函数(){
jQuery(this).data('count',parseInt(jQuery(this.html(),10));
jQuery(this.html('0');
计数(jQuery(this));
});
});

但是,它只显示逗号前的第一个数字。。。即22256244=22将显示。。。所以我尝试使用LocalString:

<script>
   var number = 22874098;
   number.toLocaleString(); // "22,874,098"
</script>

var编号=22874098;
number.toLocaleString();//"22,874,098"
这失败了,只是不断增加。。。。。也不包括逗号

谁能告诉我如何让数字停止增长,但更重要的是,显示逗号

现在干杯

编辑-刚刚尝试了以下操作:

    <script>
      $.fn.waypoint.defaults = {
  context: window,
  continuous: false,
  enabled: true,
  horizontal: false,
  offset: 0,
  triggerOnce: true
}

$('.facts_waypoint').waypoint(function(direction) {  
function count($this){
    var current = parseInt($this.text().replace(/[^0-9]/g, ''), 10);
    if (current >= 1000000) {current = current +493023;}
    else if (current >= 10000) {current = current +43749;}
    else if (current >= 1000) { current = current + 7369;}
    else { if (current >= 100) { current = current + 197;}
            else {current = current + 1}
    }
    /*current = current + 100;  Where 1 is increment */

    $this.text(++current);
    if(current > $this.data('count')){
        $this.text($this.data('count'));
    } else {
        setTimeout(function(){count($this)}, 50);
    }
}
jQuery(".count_one, .count_two, .count_three").each(function() {
  jQuery(this).data('count', parseInt(jQuery(this).text(), 10));
  jQuery(this).text('0');
  count(jQuery(this));
});
});
</script>

$.fn.waypoint.defaults={
上下文:窗口,
连续:假,
启用:对,
水平:错,
偏移量:0,
触发:对
}
$('.facts_航路点')。航路点(函数(方向){
函数计数($this){
var current=parseInt($this.text().replace(/[^0-9]/g',),10);
如果(当前>=1000000){current=current+493023;}
如果(当前>=10000){current=current+43749;}
如果(当前>=1000){current=current+7369;}
else{if(current>=100){current=current+197;}
else{current=current+1}
}
/*电流=电流+100;其中1为增量*/
$this.text(++当前);
如果(当前>$this.data('count')){
$this.text($this.data('count'));
}否则{
setTimeout(函数(){count($this)},50);
}
}
jQuery(“.count\u one、.count\u two、.count\u three”)。每个(函数(){
jQuery(this).data('count',parseInt(jQuery(this).text(),10));
jQuery(this.text('0');
计数(jQuery(this));
});
});

对于逗号问题,在执行parseInt之前,需要从值中去掉非数字字符。我建议使用
$this.text()
而不是
$this.html()
并在输入上使用正则表达式

var current = parseInt($this.text().replace(/,/g, ''), 10);
这将仅用空字符串替换输入中的逗号,留下数字和小数分隔符。因为您只对整数感兴趣,所以可以采用正则表达式的另一种方法删除所有非数字字符

var current = parseInt($this.text().replace(/[^0-9]/g, ''), 10);
解析或格式化字段值时,需要去掉或添加逗号,并在整个代码路径中执行
text()
html()
调用。我浏览了代码,并调整了一些内容,使其更具可读性,并更清晰地分离出解析。尝试以下方法:

$('.facts_waypoint').waypoint(function(direction) {  
    function parse(text) {
        return parseInt(text.replace(/[^0-9]/g, ''), 10);
    }

    function count($this) {
        var current = parse($this.text());
        if (current >= 1000000) {
            current = current + 493023;
        } else if (current >= 10000) {
            current = current + 43749;
        } else if (current >= 1000) { 
            current = current + 7369;
        } else if (current >= 100) { 
            current = current + 197;
        } else {
            current = current + 1;
        }

        $this.text((++current).toLocaleString());
        if (current < $this.data('count')) {
            setTimeout(function() { 
                count($this) 
            }, 50);
        }
    }

    jQuery(".count_one, .count_two, .count_three").each(function() {  
        var $this = jQuery(this);
        $this.data('count', parse($this.text()));
        $this.text('0');
        count($this);
    });
});
$('.facts\u航路点')。航路点(功能(方向){
函数解析(文本){
返回parseInt(text.replace(/[^0-9]/g',),10);
}
函数计数($this){
var current=parse($this.text());
如果(当前>=1000000){
电流=电流+493023;
}否则如果(当前>=10000){
电流=电流+43749;
}如果(当前>=1000){
电流=电流+7369;
}如果(当前>=100){
电流=电流+197;
}否则{
电流=电流+1;
}
$this.text(++current.toLocaleString());
如果(当前<$this.data('count')){
setTimeout(函数(){
计数($this)
}, 50);
}
}
jQuery(“.count\u one、.count\u two、.count\u three”).each(function(){
var$this=jQuery(this);
$this.data('count',parse($this.text());
$this.text('0');
计数($这个);
});
});

对于逗号问题,在执行parseInt之前,需要从值中去掉非数字字符。我建议使用
$this.text()
而不是
$this.html()
并在输入上使用正则表达式

var current = parseInt($this.text().replace(/,/g, ''), 10);
这将仅用空字符串替换输入中的逗号,留下数字和小数分隔符。因为您只对整数感兴趣,所以可以采用正则表达式的另一种方法删除所有非数字字符

var current = parseInt($this.text().replace(/[^0-9]/g, ''), 10);
解析或格式化字段值时,需要去掉或添加逗号,并在整个代码路径中执行
text()
html()
调用。我浏览了代码,并调整了一些内容,使其更具可读性,并更清晰地分离出解析。尝试以下方法:

$('.facts_waypoint').waypoint(function(direction) {  
    function parse(text) {
        return parseInt(text.replace(/[^0-9]/g, ''), 10);
    }

    function count($this) {
        var current = parse($this.text());
        if (current >= 1000000) {
            current = current + 493023;
        } else if (current >= 10000) {
            current = current + 43749;
        } else if (current >= 1000) { 
            current = current + 7369;
        } else if (current >= 100) { 
            current = current + 197;
        } else {
            current = current + 1;
        }

        $this.text((++current).toLocaleString());
        if (current < $this.data('count')) {
            setTimeout(function() { 
                count($this) 
            }, 50);
        }
    }

    jQuery(".count_one, .count_two, .count_three").each(function() {  
        var $this = jQuery(this);
        $this.data('count', parse($this.text()));
        $this.text('0');
        count($this);
    });
});
$('.facts\u航路点')。航路点(功能(方向){
函数解析(文本){
返回parseInt(text.replace(/[^0-9]/g',),10);
}
函数计数($this){
var current=parse($this.text());
如果(当前>=1000000){
电流=电流+493023;
}否则如果(当前>=10000){
电流=电流+43749;
}如果(当前>=1000){
电流=电流+7369;
}如果(当前>=100){
电流=电流+197;
}否则{
电流=电流+1;
}
$this.text(++current.toLocaleString());