脚本在 Chrome 中工作,但在 firefox 中不工作

Script working in Chrome, but not in firefox

我正在学习 JS 教程,并且刚刚制作了一个抵押贷款计算器,它在 Chrome 中非常有用,但在 Firefox 中却没有任何作用。我能得到一些帮助吗? 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

如果您想在所有浏览器中使用它,一个干净的解决方案是创建一个 TextNode 并将其附加到视图。

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));
}

查看更多详情: