使用jQuery将div值添加到div类

Fut*_*ebs 0 jquery

我有一个同类的div列表.每个div都有一个我需要添加小数位的数字.html是这样的:

<div class="demo">14214</div>
<div class="demo">23455</div>
<div class="demo">45645</div>
<div class="demo">76554</div>
<div class="demo">77655</div>
Run Code Online (Sandbox Code Playgroud)

我需要使用jQuery返回结果:

<div class="demo">14.214</div>
<div class="demo">23.455</div>
<div class="demo">45.645</div>
<div class="demo">76.554</div>
<div class="demo">77.655</div>
Run Code Online (Sandbox Code Playgroud)

我一直在研究的jQuery代码是:

$.each($('.demo'), function (index, value) { 

var number = parseInt($(this).text());
var addcommas = new Intl.NumberFormat().format(number);

var adddots = addcommas.toString().replace(',', '.');

$(this).innerHTML = adddots;

});
Run Code Online (Sandbox Code Playgroud)

JS小提琴

有人可以帮忙吗?

ADy*_*son 6

你的算法很好,你唯一的错误就是你使用了错误的函数来尝试更新div.将最终值记录adddots到控制台会在调试时显示此信息.

innerHTML适用于DOM元素,而不是jQuery对象.jQuery相当于.html().

所以你可以做到

$(this).html(adddots);
Run Code Online (Sandbox Code Playgroud)

有关演示,请参阅https://jsfiddle.net/o65rchzL/62/.