首页 > 解决方案 > getElementById 没有在脚本中返回任何内容或问题?

问题描述

我正在制作一个从输入字段返回值的程序,然后根据条件将字段字符串更改为 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>

标签: javascripthtmldebugging

解决方案


在类名前加一个点。

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

推荐阅读