未捕获的typerror无法在window上设置null的属性“ onclick”。出现卸载错误。不知道要解决什么。我敢肯定这是一个简单的解决方法,但是我在学校学习,对我来说,所有这些都是很新的。任何帮助将不胜感激。练习的重点是使用控制台面板在代码中查找错误,然后进行修复。错误显示在此行上:
$("join_List").onclick = joinList;
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Join Email List</title>
<link rel="stylesheet" href="email_list.css">
<script src="email_list.js"></script>
</head>
<body>
<main>
<h1>Please join our email list</h1>
<form id="email_form" name="email_form" action="join.html" method="get">
<label for="email_address1">Email Address:</label>
<input type="text" id="email_address1" name="email_address1"><br>
<label for="email_address2">Re-enter Email Address:</label>
<input type="text" id="email_address2" name="email_address2"><br>
<label for="first_name">First Name</label>
<input type="text" id="first_name" name="first_name"><br>
<label> </label>
<input type="button" id="join_list" value="Join our List"><br>
</form>
</main>
<script src="email_list.js"></script>
JavaScript
"use strict";
var $ = function(id) {
return document.getElementById(id);
};
var joinList = function() {
var emailAddress1 = $("email_address1").value;
var emailAddress2 = $("email_address2").value;
var firstName = $("first_name").value;
var errorMessage = "";
if (emailAddress1 == "") {
errorMessage = "First email address entry required";
$("email_address1").focus();
} else if (emailAddress2 == "") {
errorMessage = "Second email address entry required";
$("email_address2").focus();
} else if (emailAddress2 != emailAddress1) {
errorMessage = "Email address entries must match";
$("email_address2").focus();
} else if (firstName == "") {
errorMessage = "First name entry required";
$("first_name").focus();
}
if (errorMessage = "") {
$("email_form").submit();
} else {
alert(errorMessage);
}
};
window.onload = function() {
$("join_List").onclick = joinList;
$("email_address1").focus();
};