javascript - 如何将文本转换为 Float 并在新的 div 中打印帐户的结果

标签 javascript jquery html price

我正在尝试在 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/

相关文章:

javascript - jQuery OnLoad 页面滚动问题

javascript - 如何用jquery替换一个字符串中可以变化的字符

javascript - 灯箱 Facebook iframe,视频嵌入

javascript - 网页上的按钮将变为 'behind' 元素,无论 Z 索引如何,位置都定义为固定

javascript - 内容不会显示在 jQuery 的切换按钮上

html - 文本不会在标题中居中

html - CSS - 使图像适合特定大小的 div

javascript - 我如何在没有 bundler 的情况下使用 react.js?

javascript - 如何在循环中添加迭代函数来计算选择数组对象值的日期的持续时间?

javascript - 无法将 `Symbol` 用于 paper.js 的 `Group`