【问题标题】:Script working in Chrome, but not in firefox脚本在 Chrome 中有效,但在 Firefox 中无效
【发布时间】:2015-01-06 14:31:09
【问题描述】:

我正在学习一个 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);
};

【问题讨论】:

  • 更新您的 Firefox。除了供应商函数和前缀之外,firefox 在计算上与 chrome 没有什么不同!
  • @surajrawat Firefox 不同...

标签: javascript cross-browser


【解决方案1】:

将payments.innerText 替换为payments.textContent

【讨论】:

  • 一些旧浏览器需要innerText :) 设置两者...或设置innerHTML
【解决方案2】:

如果您想在所有浏览器上使用它,一个干净的解决方案是创建一个 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));
}

查看更多详情:https://stackoverflow.com/a/1359834/891092

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多