简体   繁体   English

如何使用jQuery删除跨度数值的25%?

[英]How to remove 25% of number value of span using jQuery?

I want to remove 25% from a price and then show the sum in a new span. 我想从价格中删除25% ,然后在新的跨度中显示总和。 Something like this: 像这样:

price * 0.8 = newPrice //edited

This is my span: 这是我的范围:

<span id="ctl00_main_ctl00_ctl00_labPrice" class="price" itemprop="price">21.188</span>

Here is what I've come up with so far... 到目前为止,这是我要提出的...

$(".purchase-block-price" ).each(function() {

    // Show price ex vat
    var price = parseInt($(".price").text().trim().replace(".",""));
    var newprice = price * 0.8;

    $(newprice).appendTo(".purchase-block-price");
});

Is it even possible? 可能吗?

Sure, get the value (in this case with text() ) and multiply by 0.75. 当然,获取值(在这种情况下为text() )并乘以0.75。

 var value = parseFloat( $("#ctl00_main_ctl00_ctl00_labPrice").text() ); var newValue = value * 0.75; console.log( newValue ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="ctl00_main_ctl00_ctl00_labPrice" class="price" itemprop="price">21.188</span> 

Try : 尝试:

 var oldprice = $('#ctl00_main_ctl00_ctl00_labPrice').text(); var newprice = oldprice - (oldprice * 0.20); console.log(newprice); /* Do whatever you want with your new price*/ 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="ctl00_main_ctl00_ctl00_labPrice" class="price" itemprop="price">21.188</span> 

Use .text( function ) to replacing current value of span with new value. 使用.text( function )将span的当前值替换为新值。

$("#ctl00_main_ctl00_ctl00_labPrice").text(function(i, text){
  return text - (text * 0.25); // or text * 0.75
});

 $("#ctl00_main_ctl00_ctl00_labPrice").text(function(i, text){ return text * 0.75; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="ctl00_main_ctl00_ctl00_labPrice" class="price" itemprop="price">21.188</span> 

To achieve this you can loop over the .price elements, take the text() value from them and convert it to a floating point number using parseFloat() . 为此,您可以遍历.price元素,从它们中获取text()值,然后使用parseFloat()将其转换为浮点数。 From there you can calculate the value, minus 20% and insert a new span containing the value. 从那里您可以计算出负20%的值,并插入一个包含该值的新span Try this: 尝试这个:

 $(".purchase-block-price .price").each(function() { var price = parseFloat($(this).text().trim()); var newprice = price * 0.75; $('<span class="newprice">' + newprice + '</span>').insertAfter(this); }); 
 .price { margin-top: 10px; display: block; } .newprice { color: red; display: block; font-size: 1.2em; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="purchase-block-price"> <span id="ctl00_main_ctl00_ctl00_labPrice" class="price" itemprop="price">21.188</span> </div> <div class="purchase-block-price"> <span id="ctl00_main_ctl00_ctl00_labPrice" class="price" itemprop="price">132.299</span> </div> 

Note that you can use the .toFixed(2) method to round the calculated price values if required. 请注意,如果需要,可以使用.toFixed(2)方法四舍五入计算出的价格值。

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

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