javascript - 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>
解决方案
在类名前加一个点。
document.querySelector(".output-box").innerHTML = "You can become Uber !";
推荐阅读
- angular - 如何在 canActivate Guard 中订阅两个 API
- cyclejs - 如何在 Cycle.JS 登录表单组件中对 HTTP 请求进行排序?
- python - 在 Windows 上使用 pycharm 在 git bash 中激活 virtualenv
- reactjs - 数据道具从状态更改后React-Table不更新
- reactjs - React Native Component 不会随着状态变化而更新
- ios - SwiftUI Animation.fluidSpring() 替换
- javascript - JavaScript 新手需要“未定义”结果的帮助
- image - 由于错误的 EXIF 数据,JPEG 照片在插入 Adobe LiveCycle 图像字段时不会显示
- python - Scrapy登录适用于某些网站,但不适用于其他网站
- vue.js - 如何使用 vue.js 在选择中设置默认选定值?