gpt4 book ai didi

javascript - getElementById 在脚本中没有返回任何内容或问题?

转载 作者:行者123 更新时间:2023-12-04 00:53:10 26 4
gpt4 key购买 nike

我正在制作一个程序,该程序从输入字段返回值,然后根据条件将字段字符串更改为 x 结果。非常感谢您的帮助,因为这里的成员过去一直给我很大帮助。调试器抛出此错误,当然没有任何效果:

script.js:22 Uncaught TypeError: Cannot set property 'innerHTML' of null
at uberChecker (script.js:22)
at HTMLButtonElement.onclick (index.html:25)

这是我的代码(这里是初学者):

JS:

    var username = document.getElementById("myUsername").value;
var groupIs = document.getElementById("myGroup").value;
var warnings = document.getElementById("myWarning").value;
var postCount = document.getElementById("myPostCount").value;





function uberChecker() {

if ((groupIs != ('Uber' || 'uber') && postCount > '1000') && warnings === '0') {

document.querySelector("output-box").innerHTML = "You can become Uber !";
} else if (postCount < '1000' && warnings === '0') {
document.querySelector("output-box").innerHTML = (username + ' You have ' + postCount + ' posts, you do not meet the requirements');

} else if (warnings != '0' && postCount > '1000') {
document.querySelector("output-box").innerHTML = (username + ' You cannot upgrade with ' + warnings + ' warning')
} else if (postCount < '1000' && warnings != '0') {
document.querySelector("output-box").innerHTML = (username + ' you have ' + postCount + ' posts which is less than 1000 and you have ' + warnings + '% warning. You cannot upgrade');
} else {
document.querySelector("output-box").innerHTML = (username + ' You are already Uber');
}

}

HTML:

<!DOCTYPE html>

<html>

<head>
<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

<div class="blue-box"></div>

<input type="text" placeholder="Type username" id="myUsername" class="user" >
<input type="text" placeholder="Type user group" id="myGroup" class="group">
<input type="text" placeholder="Type post count" id="myPostCount" class="post">
<input type="text" placeholder="Type warning level" id="myWarning" class="warning">

<div class="UsernameText">Username</div>
<div class="groupText">Group</div>
<div class="postText">Posts</div>
<div class="warningText">Warning</div>


<button type="button" onclick="uberChecker();" class="black-box">Check if you are upgradeable</button>

<div class="output-box">Result will display here</div>

<script src="script.js"></script>

</body>

</html>

最佳答案

在类名前加一个点。

document.querySelector(".output-box").innerHTML = "You can become Uber !";

关于javascript - getElementById 在脚本中没有返回任何内容或问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61858185/

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