首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.each函数中带有逗号的格式编号

.each函数中带有逗号的格式编号
EN

Stack Overflow用户
提问于 2016-03-21 20:09:05
回答 1查看 136关注 0票数 1

所以我有一些js,它通过每日汇率引擎来转换div类的编号。它的输出是正确的,就像它应该的那样,我现在正试图将这个数字分开,它使用jQuery和我在做一些研究时发现的函数来输出。我试图使用.innerHTML方法将这个数字输入到函数中。我有一个函数提醒一个转换的数字,但我有多个元素,这个函数应该运行,所以使用了一个.each函数--这是某些东西不能工作的地方。我没有收到警报,所以我认为.each代码有问题。

有人能看到任何可能导致它的东西吗?

完整的代码如下:

代码语言:javascript
复制
<script src="https://raw.githubusercontent.com/openexchangerates/money.js/master/money.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>


<div class="hello">
  2300
</div>

<div class="hello">
  52400
</div>




<script>

    function ReplaceNumberWithCommas(yourNumber) {
    //Seperates the components of the number
    var n= yourNumber.toString().split(".");
    //Comma-fies the first part
    n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    //Combines the two sections
    return n.join(".");
}

    $(".hello").each(function() {

    var currentDiv = $(this);
    var currentPrice = currentDiv.text();

    var demo = function(data) {
    fx.rates = data.rates
    var rate = fx(currentPrice).from("GBP").to("USD");
    currentDiv.html("<div>"+currentPrice +"</div><div id='converted'> " +rate.toFixed(0)+"</div>");
        //alert("Product Costs" + rate.toFixed(4))

}
    $.getJSON("http://api.fixer.io/latest", demo);
});


    $("#converted").each(function() {
        var convertedPrice = $(this.innerHTML);
        function runThis() { alert( ReplaceNumberWithCommas(convertedPrice)) }
        setTimeout (runThis, 100);
    });

</script> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-21 20:43:05

我觉得原因是

$("#converted").each(function() { var convertedPrice = $(this.innerHTML); function runThis() { alert( ReplaceNumberWithCommas(convertedPrice)) } setTimeout (runThis, 100); });

在创建转换元素之前发生的事情。因为你把创造放进了一个get电话里。

我建议你把这个放回你的电话。

就像这样

代码语言:javascript
复制
  function ReplaceNumberWithCommas(yourNumber) {
    //Seperates the components of the number
    var n = yourNumber.toString().split(".");
    //Comma-fies the first part
    n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    //Combines the two sections
    return n.join(".");
  }
    var currentDiv = $(this);
    var currentPrice = currentDiv.text();

    var demo = function(data) {
      fx.rates = data.rates
      $(".hello").each(function() {
        var currentDiv = $(this);
        var currentPrice = currentDiv.text();
        var rate = fx(currentPrice).from("GBP").to("USD");
        currentDiv.html("<div>" + currentPrice + "</div><div class='converted'> " + rate.toFixed(0) + "</div>");
        //alert("Product Costs" + rate.toFixed(4))
      });

      $(".converted").each(function() {
        var convertedPrice = $(this).html();
        console.log(ReplaceNumberWithCommas(convertedPrice));
      });
    }    
  $.getJSON("https://api.fixer.io/latest", demo);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36140598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档