gpt4 book ai didi

javascript - 未捕获类型错误: Cannot set property 'onClick' of null

转载 作者:行者123 更新时间:2023-12-02 17:53:01 25 4
gpt4 key购买 nike

当我在控制台中调用 btn.onClick 时,出现此错误:{未捕获的类型错误:无法将属性“onClick”设置为 null }

代码在这里:

==============html==================

<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<div class="container">
<h3 class="billboard">Wacky Expressions Assignment</h3>
<p><b>Cost of Shoe Strings</b>:<input type="text" id="inCost"/></p>
<p><b>Down Payment</b>:<input type="text" id="inDown"/></p>
<p><b>APR (Intrest)</b>:<input type="text" id="inAPR"/></p>
<p><b>Term (How long)</b>:<input type="text" id="inTime"/></p>
<p><button id="btnCalculate">Calculate Payments</button></p>
<p><b>Monthly Payments</b>:<span id="outMonthly"/></p>
</div>

<script language="JavaScript" src="js/script.js" type="text/javascript"></script>
</body>
</html>

==============JS==================

//The Formula: c = (r * p) /  (1 - (Math.pow((1 + r), (-n))));
//@param p float Amount borrowed
//@param r interest, as a percentage
//@param n term in years
function calculateMortgage(p, r, n ) { //setting function for mortgage calculator

//converting this percentage to a decimal
var r = percentToDecimal(r);

//convert years to months
n = yearsToMonths(n); //parameters set for n = conversion of years to months

//convert data with formula for obtaining monthly payments
var pmt = (r * p) / (1 - (Math.pow((1 + r), (-n))));

return parseFloat(pmt.toFixed(2));

return monthlyPayments; //returning to variabale monthly
}

function percentToDecimal(percent) { /
return (percent/12) / 100; //assigning calculation for conversion of input
}

function postPayments(payment) {
var htmlEl = document.getElementById("outMonthly");

htmlEl.innerText = "$" + payment;
}

function yearsToMonths(year) { //function setup for converting year to months
return year * 12; //assigning calculation for years to months
}

var btn = document.getElementById("btnCalculate");
btn.onclick = function calculateMortgage( p, r , n) {
var cost = document.getElementById("inCost").value; //declaring variables for cost
var downPayment = document.getElementById("inDown").value;
var interest = document.getElementById("inAPR").value; //declaring variable for intrest
var term = document.getElementById("inTime").value; //declaring variable for term

var amountBorrowed = cost - downPayment; //declaring varibales for amount borrowed

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

postPayments(pmt);
}

//console.log(cost, downPayment, interest, term);
//console.log("R", p); //call out log for para p
//console.log("R", r); //call out log for para r
//console.log("R", n); //call out log for para n

最佳答案

HTML 文档从上到下加载。该脚本在 DOM 加载之前执行,因此在执行脚本时,页面上不存在 btnCalculate

如果您将脚本放在页面底部,这应该可以解决您的问题。

关于javascript - 未捕获类型错误: Cannot set property 'onClick' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com