需要在JavaScript中将字符串转换为int

时间:2018-01-23 05:30:41

标签: javascript html

<div id="remainingfunds">
  <p>Remaining Funds: $<span id="remainingfundsnumber">500</span></p>
</div><!--remainingfunds-->

<div id="subtotal">
  <p>Subtotal: $<span id="subtotalnumber">0</span></p>
</div><!--subtotal-->

remainingfundsnumber=parseInt(remainingfundsnumber);

subtotal = parseInt(subtotalnumber);

显然不那么简单。请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用document.getElementById()获取所需的元素并使用所需的ID innerHTML获取其内部html数据

remainingfundsnumber = parseInt(document.getElementById("remainingfundsnumber").innerHTML);
subtotal = parseInt(document.getElementById("subtotalnumber").innerHTML);
<div id="remainingfunds">
  <p>Remaining Funds: $<span id="remainingfundsnumber">500</span></p>
</div><!--remainingfunds-->

<div id="subtotal">
  <p>Subtotal: $<span id="subtotalnumber">0</span></p>
</div><!--subtotal-->