我对JavaScript完全陌生,我不知道为什么会收到此错误:Uncaught TypeError:无法读取null的属性'addEventListener'。
我遵循了针对类似问题的建议,并将包含我的JavaScript的文件放在我的身体底部,但这并不能解决问题。我还尝试将按钮设置为onclick = checkValidity(),但这也不起作用。
1.dispatch_group_t group = dispatch_group_create();
2.dispatch_group_enter(group);
3.[self exportVideoAsset:avAsset withRange:CMTimeRangeMake(start1, duration1) inGCDGroup:group];
4.dispatch_group_enter(group);
5.[self exportVideoAsset:avAsset withRange:CMTimeRangeMake(start2, duration2) inGCDGroup:group];
我想要代码做的是检查inputUsername和inputPswrd中的输入是否与if ... else函数中指定的输入匹配,如果是,则打开一个新窗口;否则会提示错误消息。
编辑:如果将上面所有代码移动到JS文件的顶部,它将按预期工作并打开一个新窗口,但是该新页面的代码将无法运行(但新页面代码会运行运行它在我的JS文件的顶部)。当新窗口打开时,我收到的错误消息是第1行:Uncaught TypeError:无法在Script.js:1处读取null的属性'value'。
附加代码: HTML for HTML页面2:
document.getElementById("loginButton").addEventListener("click",
checkValidity);
function checkValidity() {
var usrName = document.getElementById("inputUsername").value;
var usrPswd = document.getElementById("inputPswrd").value;
if(usrName == "admin" && usrPswd == "123"){
window.open("HtmlPage2.html");
}
else {
alert("Your username and password are incorrect.");
}
}
and the HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Document</title>
</head>
<body>
<div class=wrap>
<input type="text" id="inputUsername">
<p>Username</p>
<input type="password" id="inputPswrd">
<p>Password</p>
<button id="loginButton">Log in</button>
<p id="login"></p>
</div>
<script type="text/javascript" src="Script.js" async></script>
</body>
</html>
页面2的HTML开头
document.getElementById("greeting").innerHTML="Welcome";
document.getElementById("productButtonOne").addEventListener("click",
addToCart);
document.getElementById("productButtonTwo").addEventListener("click",
addToCart);
document.getElementById("productButtonThree").addEventListener("click",
addToCart);
var clicks = 0;
function addToCart() {
clicks = clicks + 1;
document.getElementById("cartProductsTotal").innerHTML = "You have " +
clicks + " items in your shopping cart.";
}
答案 0 :(得分:-1)
将scirpt标签放置在可以使用的正文之后
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Document</title>
</head>
<body>
<div class=wrap>
<input type="text" id="inputUsername">
<p>Username</p>
<input type="password" id="inputPswrd">
<p>Password</p>
<button id="loginButton">Log in</button>
<p id="login"></p>
</div>
<script type="text/javascript" src="Script.js" async></script>
</body>
<script>
document.getElementById("loginButton").addEventListener("click", checkValidity);
function checkValidity() {
var usrName = document.getElementById("inputUsername").value;
var usrPswd = document.getElementById("inputPswrd").value;
if(usrName == "admin" && usrPswd == "123"){
window.open("HtmlPage2.html");
}
else {
alert("Your username and password are incorrect.");
}
}
</script>
</html>
答案 1 :(得分:-2)
删除此行<script type="text/javascript" src="Script.js" async></script>
,我认为您的代码加载了错误的脚本。