3 回答

TA贡献1898条经验 获得超8个赞
您的代码有 2 个问题:
“NullReference: reference not found”如果您将
<script></script>
标记放在<head>...</head>
您需要eventListener
检查是否加载了 DOM。您可以使用eventListener
类似的加载DOMContentLoaded来执行此操作。“打字错误”你
document.
打电话时忘记了document.getElementById("Display")
<!DOCTYPE html>
<html>
<head>
<!-- ALL OF THE INFO GOES HERE -->
<title> Pay calculater </title>
<script>
const setup = () => {
let xy = 3; //put in number of yards here
let xb = 1; //put in number of times bluffs done here
let xn = 2; //put in number of times newbern done here
let y = 5;
let b = 25;
let n = 15;
const pay = y * xy + b * xb + n * xn;
document.getElementById("Display").innerHTML = pay;
}
window.addEventListener('load', setup);
</script>
</head>
<!-- ALL OF THE INFO GOES HERE -->
<body>
<p id="Display"></p>
</body>
</html>

TA贡献1853条经验 获得超9个赞
前缀getElementById
withdocument.
因为它是document
对象上的一个方法。
document.getElementById("Display");

TA贡献1804条经验 获得超8个赞
您的脚本在加载整个页面之前正在运行。当它运行时,没有要更新的“显示”元素。您需要在它存在后对其进行更新。您的浏览器控制台将有很好的错误消息,说明出了什么问题。
添加回答
举报