简体   繁体   中英

Script working in Chrome, but not in firefox

I'm following a JS tutorial and just made a mortgage calculator that works like a charm in Chrome, but it does nothing in Firefox. Can I get some help figuring this out please? Here's the whole thing :

// Formula: c = ((p * r) *  math.pow((1 + r), n)) / (math.pow((1 + r), n) - 1)

//@param p float Amount borrowed
//@param r interest as percentage
//@param n term in years

function percentToDecimal(percent) {
return (percent / 12) / 100;
}

function yearsToMonths(years) {
return years * 12;
}

function calculateMortgage(p, r, n) {
// convert this percentage to decimal:
r = percentToDecimal(r);
n = yearsToMonths(n);

var pmt = ((p * r) * Math.pow((1 + r), n)) / (Math.pow((1 + r), n) - 1);

return parseFloat(pmt.toFixed(2));

}

function postpayments(payment) {
var payments = document.getElementById("outMonthly");
payments.innerText = "$" + payment;
}

var btn = document.getElementById("btnCalculate");
btn.onclick = function () {
var cost = document.getElementById("inCost").value;
var downpayment = document.getElementById("inDown").value;
var interest = document.getElementById("inAPR").value;
var term = document.getElementById("inPeriod").value;

var amountBorrowed = cost - downpayment;

var pmt = calculateMortgage(amountBorrowed, interest, term);

postpayments(pmt);
};

payments.innerText替换为payments.textContent

If you want to work it across all browsers, a clean solution would be to create a TextNode and attach it to the view.

function postpayments(payment) {
    var payments = document.getElementById("outMonthly");
    while (payments.firstChild!==null)
        element.removeChild(payments.firstChild); // remove all existing content
    payments.appendChild(document.createTextNode("$" + payment));
}

Check this for more details: https://stackoverflow.com/a/1359834/891092

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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