【发布时间】:2018-05-25 23:07:19
【问题描述】:
当我将我的 Javascript 链接到多个页面时,我对为什么会收到此错误感到困惑。我正在尝试使用 Javascript 来更改表单标签的颜色,因为它的输入字段包含文本。它在登录部分工作正常,但当我将它链接到 SignUp.html 文件时,它不起作用并给我一个错误。这是我在 codepen 上的项目的链接,如果你不想举例说明我在说什么。非常感谢任何帮助!
// LOGIN INPUT VARIABLES
let usernameInput = document.querySelector("#usernameInput");
let passwordInput = document.querySelector("#passwordInput");
// SIGN UP INPUT VARIABLES
let firstNameInput = document.querySelector("#firstNameInput");
let lastNameInput = document.querySelector("#lastNameInput");
usernameInput.addEventListener("input", function() {
document.querySelector("#usernameLabel").style.color = "#98589e";
if (usernameInput.value == '') {
document.querySelector("#usernameLabel").style.color = "#b9c0c8";
}
});
passwordInput.addEventListener("input", function() {
document.querySelector("#passwordLabel").style.color = "#98589e";
if (passwordInput.value == '') {
document.querySelector("#passwordLabel").style.color = "#b9c0c8";
}
});
firstNameInput.addEventListener("input", function() {
document.querySelector("#firstNameLabel").style.color = "#98589e";
if (firstNameInput.value == '') {
document.querySelector("#firstNameLabel").style.color = "#b9c0c8";
}
});
lastNameInput.addEventListener("input", function() {
document.querySelector("#lastNameLabel").style.color = "#98589e";
if (lastNameInput.value == '') {
document.querySelector("#lastNameLabel").style.color = "#b9c0c8";
}
});
【问题讨论】:
-
你能附上你得到的具体错误信息吗?
-
您得到的错误是,当您尝试查找输入标签时,它返回 null。你需要看看是不是命名问题
-
@NocNit 这是错误消息 -> Uncaught TypeError: Cannot read property 'addEventListener' of null at scripts.js:8
标签: javascript html css dom-events