Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在本例中,如何在Javascript中添加分隔数千的空格_Javascript_Jquery - Fatal编程技术网

在本例中,如何在Javascript中添加分隔数千的空格

在本例中,如何在Javascript中添加分隔数千的空格,javascript,jquery,Javascript,Jquery,这里有两个函数。一个添加了一个,用于分隔千,如1234->1234。和一个增加的功能 增加的功能只是打印123456,我想结合这些功能,我想我可以更改: $this.html++当前版本 致: $this.htmladspaces++当前版本 但它不起作用。请帮帮我,我怎样才能解决这个问题 function addSpaces(nStr) { nStr += ""; x = nStr.split("."); x1 = x[0]; x2 = x.length >

这里有两个函数。一个添加了一个,用于分隔千,如1234->1234。和一个增加的功能

增加的功能只是打印123456,我想结合这些功能,我想我可以更改:

$this.html++当前版本

致: $this.htmladspaces++当前版本

但它不起作用。请帮帮我,我怎样才能解决这个问题

function addSpaces(nStr)
{
    nStr += "";
    x = nStr.split(".");
    x1 = x[0];
    x2 = x.length > 1 ? "." + x[1] : "";
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, "$1" + " " + "$2");
    }
    return x1 + x2;
}   


function count($this) {

    var current = parseInt($this.html(), 10);
    current = current + 13 /* This is increment */

    $this.html(++current);
    if (current > $this.data("count")) {
        $this.html($this.data("count"));
    } else {
        setTimeout(function() { count($this); }, 100);
    }

}
更新我修改了你的JSFIDLE

由于当前值将被一次又一次地从格式化值中解析,因此我们需要从中删除空格

current = parseInt(($this.html()).split(' ').join(''), 10)
此外,您还需要在名为nextString的变量下保留递增的current的字符串值的跟踪

您希望您的号码最多按3位数字分组。问题是,如果3不除以字符串的长度,则可能有一个余数。一旦将字符串的剩余部分分离出来,就可以将所有其他部分按3分组

更新我修改了你的JSFIDLE

由于当前值将被一次又一次地从格式化值中解析,因此我们需要从中删除空格

current = parseInt(($this.html()).split(' ').join(''), 10)
此外,您还需要在名为nextString的变量下保留递增的current的字符串值的跟踪

您希望您的号码最多按3位数字分组。问题是,如果3不除以字符串的长度,则可能有一个余数。一旦将字符串的剩余部分分离出来,就可以将所有其他部分按3分组


第一个可行,第二个可以使用下面的方法

<div class="count">1234</div>

这里是工作演示:单击演示上的第一个div,第一个可以工作,第二个可以使用下面的内容

<div class="count">1234</div>

这里是正在运行的演示:单击演示中的第一个div,或者您可以使用toLocaleString之类的工具,如果您需要的话:

var number = 3500;

console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale

var number = 123456.789;

// German uses comma as decimal separator and period for thousands
alert(number.toLocaleString("de-DE"));
// → 123.456,789

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(number.toLocaleString("ar-EG"));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
alert(number.toLocaleString("en-IN"));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
alert(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → 123.456,789

请参阅:

或者,如果您需要,也可以使用toLocaleString之类的工具:

var number = 3500;

console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale

var number = 123456.789;

// German uses comma as decimal separator and period for thousands
alert(number.toLocaleString("de-DE"));
// → 123.456,789

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(number.toLocaleString("ar-EG"));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
alert(number.toLocaleString("en-IN"));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
alert(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → 123.456,789

请参阅:

在上面的示例中,我如何将其与count$this函数一起使用?您能否给我们一个带有html示例的JSFIDLE,以便我们可以对其进行编辑?使用示例更新了我的答案如何将其与上面的示例中的count$this函数一起使用?您能否给我们一个带有html示例的JSFIDLE,以便我们可以对其进行编辑?使用示例更新了我的答案例如,使用1234567.tolocalString.replace/,/g表示千和之间的空格,使用1234567.tolocalString.replace/,/g表示千和之间的空格