如何将文本转换为浮点数并在新的 div 帐户结果中打印

How to convert text to Float and print in a new div the result of account

我正在尝试获取 div 中的字符串,删除该字符串的内容,然后在“.skuBestPrice”中获取实际价格,删除特殊字符并创建一个帐户以转换为浮动数字并在 div“.totalPrice”中完成打印此数字,但无法正常工作,这是什么问题?谢谢!!!

 /* Transform String in Number */
        $(document).ready(function() {
          function formatPrice() {
         //$('.totalPrice').empty();
         var price = parseFloat($.trim($('.skuBestPrice').html()).replace(",", "").replace("$", "").val());
         var total = (parseFloat(price)/100).toFixed(2);
         //$(".totalPrice").val(total);
           document..getElementsByClassName(".totalPrice").innerHTML = total;
          }
         //$(document).on("load", "strong", formatPrice);
        });
<!-- begin snippet: js hide: false console: true babel: false -->

将您的标记更改为

<div id="totalPrice">$ <span class="total">the final price should print here.</span></div>

<div class="price-best-price" style="display: block;">Por: 
    <strong class="skuBestPrice">$ 25,11</strong>
</div>

原因是您在 class 名称上使用 getElementById,您必须指定 ID。如果您无法更改标记,请使用 document.getElementsByClassName('totalPrice')[0]

document.getElementById("totalPrice").innerHTML = total;

document.getElementsByClassName('totalPrice')[0].innerHtml = total;

您可以使用正则表达式从字符串中提取数字,如下所示,然后将其解析为浮点数。此外,如果您想在标签之间提取值,请使用 text() 而不是 val()

/* Transform String in Number */
$(document).ready(function() {
  function formatPrice() {
 //$('.totalPrice').empty();
     var p= $(".skuBestPrice").text().replace(/[^0-9\+]/g, '');    
     var price = parseFloat(p);
     var total = (parseFloat(price)/100).toFixed(2);
     //$(".totalPrice").val(total);
      $(".total").text(total);
  }
 //$(document).on("load", "strong", formatPrice);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br /><br />
<!-- * Here are the correct value converted -->
<div class="totalPrice">$ <span class="total">the final price should print here.</span></div>

<div class="price-best-price" style="display: block;">Por: 
 <strong class="skuBestPrice">$ 25,11</strong>
</div>

很多小错误。链接功能错误的项目,错误的输入....这是简化的固定版本。

 var price = parseFloat($.trim($('.skuBestPrice').html().replace(",", "").replace("$", "")));
 var total = (parseFloat(price)/100).toFixed(2);
   document.getElementById("totalPrice").innerHTML = '$'+total;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br /><br />
<!-- * Here are the correct value converted -->
<div id="totalPrice">$ <span class="total">the final price should print here.</span></div>

<div class="price-best-price" style="display: block;">Por: 
 <strong class="skuBestPrice">$ 25,11</strong>
</div>

应该可以。对 html 和 javascript

进行一些更改

/* Transform String in Number */
$(function() {
  //function formatPrice() {
 //$('.totalPrice').empty();
  var val = $('.skuBestPrice').html();
 var price = val.replace("$", "").replace(",", "")
    //console.log(price);
    //parseFloat("554,20".replace(",", "."));
 var total = (parseFloat(price)/100).toFixed(2);
   document.getElementById("totalPrice").innerHTML = total;
  
});
  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br /><br />
<!-- * Here are the correct value converted -->
<span id="totalPrice">$</span> <span class="total">the final price should print here.</span>

<div class="price-best-price" style="display: block;">Por: 
 <strong class="skuBestPrice">$ 25,11</strong>
</div>