2017-02-16 73 views
-4

我有一個第三方估算工具,它始終如一地提供比應該高30%的結果,並且讓公司更新工具不是一種選擇。那麼,我該如何使用Javascript/jQuery DOM調整渲染的數字呢?JavaScript減少了一個類別中的百分比

<div class="general-avg"><strong>$276,000</strong></div> 

我想捕獲$ 276,000作爲一個變量,然後減少30%。任何人都可以解釋如何做到這一點? TIA

回答

0

一般SO社區期望您至少可以嘗試自己解決問題的一些代碼。這很可能是所有倒票的原因。

但我會認爲JS不是你的主要工作,你甚至不知道從哪裏開始,所以我會幫助你。

此解決方案不需要任何庫,它的工作原理沒有jQuery。

// first lets get the divs by class name 
var values = document.getElementsByClassName('general-avg'); 

// then lets go through each of the divs and select strong element inside 
[].forEach.call(values, function (item) { 
    // assumption is that your 3rd party gives you always just one strong tag inside 
    var strong  = item.getElementsByTagName('strong')[0]; 
    // once we have that element selected, take the number from inside, strip $ and remove commas, parse it to float 
    var newValue  = parseFloat(strong.innerText.replace('$','').replace(',','')); 

    // once we have that, multiply by 0.7 (I sure hope that is how you deduct 30%, otherwise this is emberassing) 
    newValue = newValue * 0.7; 

    // once we have that value we just need to format it a bit, this is shameless copy paste from 
    // http://stackoverflow.com/a/10899795/3963682 
    var formatNewValue = newValue.toString().split("."); 
    newValue = formatNewValue[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (formatNewValue[1] ? "." + formatNewValue[1] : ""); 

    // at the end, put the number back in, and add $ sign in front 
    strong.innerText = '$' + newValue; 
}); 

實施例:http://codepen.io/anon/pen/xgeQjV

+0

謝謝。我非常感謝。你是對的,JS不是我的主要工作......主要是設計和一些UI的東西。再一次,謝謝......這很有幫助。 –

+0

Miroslav,我將如何四捨五入到最接近的整數?我試過strong.innerText ='$'+ Math.round(Number(newValue));在最後一行,但這不適合我。 –

+0

沒關係,我明白了! :) 增加了「newValue = Math.round(newValue);」緊隨「newValue = newValue * 0.7;」之後 –

0

附在下面是一些JavaScript,將頁面加載運行,並會減少30%的任何標籤與類名general-avg

var Numbers = document.getElementsByClassName("general-avg"); 
for (var i = 0; i < Numbers.length; i++) { 
    Numbers[i].innerText = "$" + Math.round(Number(Numbers[i].innerText.replace(/\D/g, '')) * 0.7).toString().replace(/(...)(.)/g, "$1,$2") 
} 

代碼的演示,可以發現:HERE

+0

斷裂完全在其上是不產生圓結果號碼:https://jsfiddle.net/ btvLnqyt/4/ –

+0

Miroslav我已經修復它,使它圍繞整個n umber:jsfiddle.net/btvLnqyt/5 –

+0

OP沒有具體說明在這種情況下發生了什麼,你不能只是收集財務數字 –

0

$('.general-avg strong').each(function() { 
 
    var $avgPrice = $(this) 
 
    var number = Number($avgPrice[0].innerHTML.replace(/[^0-9\.]+/g,"")); 
 
    
 
    // the 1000s avoid rounding issues 
 
    number = number - (number * 0.3 * 1000/1000) 
 
    
 
    $avgPrice[0].innerHTML = `$${numberWithCommas(number)}` 
 
}) 
 

 
//put the commas back in 
 
function numberWithCommas(x) { 
 
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="general-avg"><strong>$100,000</strong></div> 
 
<div class="general-avg"><strong>$276,000</strong></div>

numberWithCommas功能came from here.