2015-09-15 74 views
1

我想爲我的網頁上顯示的統計信息的一些設定值....顯示逗號在JavaScript的jQuery

的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)); 
}); 
}); 

然而,只有它在逗號前面顯示第一個數字...即, 22256244 = 22將顯示...所以我試圖用LocalString:

<script> 
    var number = 22874098; 
    number.toLocaleString(); // "22,874,098" 
</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.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> 

回答

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); 
    }); 
}); 
+0

嘿 - 您的答覆,並抽出時間來幫助很多很多很多的感謝......我試圖取代的.html與.text區段 - 但它仍然存在:https://開頭volocommerce。 COM –