简体   繁体   English

如何将文本值从跨度变为数字

[英]how to make text value to number from the span

I want to make the text value to the number so I can put some calculations in the script.我想将文本值设为数字,以便可以在脚本中进行一些计算。

<p>
     <span class="shopdetailInfoName">CONSUMER</span>
     <span class="shopdetailInfoCont">
         <strike><!--/number/price_consumer/-->
             <span class="won">$</span>
         </strike>
     </span>
</p>

so as you can see the price value coming from this <!--/number/price_consummer/--> which website developer make it is work.所以你可以看到来自这个<!--/number/price_consummer/-->的价格值是由哪个网站开发人员制作的。

I try to make some script like this ->我尝试制作这样的脚本->

var a = document.getElementById ('shopdetailInfoCont');

to bring the price value as a number but .shopdetailInfoCont include "," and "$" so it bring that value to text not the number.将价格值作为数字带来,但.shopdetailInfoCont包含",""$" ,因此它将该值带入文本而不是数字。

How can I bring the value as the number?如何将值作为数字?

as a result, I want to make like this结果,我想做这样

(ex ) - product price : 45.50 $ )

if you apply -20$ discount, price will be 25.50$如果您申请-20$的折扣,价格将为25.50$

any help will be so appreciated thanks!任何帮助将不胜感激,谢谢!

var a = document.getElementById ('shopdetailInfoCont');    
floated_amount = parseFloat(a.replace(/[^0-9.-]/g, ''));

just try this regex to remove strings from your element value.只需尝试此正则表达式即可从元素值中删除字符串。

Thanks.谢谢。

Firstly, the below shouldn't work cause your class is named shopdetailInfoCont not its id.首先,以下内容不应该起作用,因为您的 class 被命名为shopdetailInfoCont而不是它的 ID。

var a = document.getElementById ('shopdetailInfoCont'); 

Second, I don't think there is a way to get a value like you wanted, but you what you can do is get 45.50 $ and with javascript parseFloat() function will return 45.50 from that text.其次,我认为没有办法获得您想要的值,但是您可以做的是获得 45.50 $ 并且使用 javascript parseFloat() function 将从该文本返回 45.50。

You're using the wrong function to retrieve your element.您使用了错误的 function 来检索您的元素。 You have the class set but not an id.您设置了 class 但没有 id。

You can use `document.getElementsByClassName() to get your element.您可以使用 `document.getElementsByClassName() 来获取您的元素。 This returns an array, so you'll access your element with the 0 index.这将返回一个数组,因此您将使用 0 索引访问您的元素。

document.getElementsByClassName("won")[0].innerHTML = "Product Price: $" + price

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM