我正在尝试在 div 中获取一个字符串,删除其中的内容,然后在“.skuBestPrice”中获取实际价格,删除特殊字符并创建一个帐户以转换为 float 并完成打印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 -->
最佳答案
很多小错误。将函数链接到错误的项目、错误的输入....这是简化的固定版本。
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>
关于javascript - 如何将文本转换为 Float 并在新的 div 中打印帐户的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44076747/