我应该如何在HTML中显示数字以便以后在javascript中计算(How should i display number in HTML for later calculation in javascript)

我试图弄清楚如何显示数字是一种正确有效的方法,以便以后在HTML中进行计算。 这是我现在能想到的,但似乎不对。

<p class = "price"> <span class ="sign">$</span> 10 </p>

后期实施包括

$("p.price") * (the desire currency rate being called)

然后它使用p.price更新整个页面

I am trying to figure out how to display number a right and efficient way for later calculation in HTML. This is what i can think of right now but doesn't seems right.

<p class = "price"> <span class ="sign">$</span> 10 </p>

Later implementation includes

$("p.price") * (the desire currency rate being called)

It then updates the whole page with the p.price

最满意答案

考虑使用数据属性 :

<p class="price" data-usd-price="10"> any markup you want </p>
 

然后,您可以根据需要对其进行格式化,然后使用以下命令访问原始值:

$("p.price").data("usd-price")
 

这里有一个更复杂的例子:

<p class="price" data-usd-price="10">foo<span class="converted"></span></p>
<p class="price" data-usd-price="30">bar<span class="converted"></span></p>
<p class="price" data-usd-price="49.99">buzz<span class="converted"></span></p>
<p class="price" data-usd-price="99.99"><span class="converted"></span></p>
 
$('p.price').each(function () {
  $(this)
    .children('span.converted')
    .html(
      $(this).data('usd-price') * 22
    )
})

Consider using data attributes:

<p class="price" data-usd-price="10"> any markup you want </p>
 

You can then format it however you like and access the raw value later with:

$("p.price").data("usd-price")
 

Here a bit more complicated example:

<p class="price" data-usd-price="10">foo<span class="converted"></span></p>
<p class="price" data-usd-price="30">bar<span class="converted"></span></p>
<p class="price" data-usd-price="49.99">buzz<span class="converted"></span></p>
<p class="price" data-usd-price="99.99"><span class="converted"></span></p>
 
$('p.price').each(function () {
  $(this)
    .children('span.converted')
    .html(
      $(this).data('usd-price') * 22
    )
})

                    
                     
          

更多推荐