首页 > 解决方案 > 如何修复未捕获的类型错误:无法读取 null 的属性“addEventListener”(与脚本文件的位置无关......我认为)

问题描述

我对 JavaScript 完全陌生,我不知道为什么会出现此错误:未捕获的类型错误:无法读取 null 的属性“addEventListener”。

我遵循了针对类似问题发布的建议,并将包含我的 JavaScript 的文件放在我身体的底部,但这并没有解决它。我还尝试制作按钮 onclick=checkValidity(),但这也不起作用。

        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>

我想要代码做的是检查 inputUsername 和 inputPswrd 中的输入是否与 if...else 函数中指定的匹配,如果是,则打开一个新窗口;否则会提示错误消息。

编辑:如果我将上面的所有代码移动到我的 JS 文件的顶部,它将按预期工作并打开一个新窗口,但是该新页面的代码将不会运行(但新页面代码确实运行它在我的 JS 文件的顶部)。新窗口打开时我收到的错误消息是第 1 行:未捕获的 TypeError:无法在 Script.js:1 处读取 null 的属性“值”。

附加代码:HTML页面2的JS:

    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.";
        }

第 2 页的 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>
        <h1 id="greeting"></h1>

        <div id="productOne" class="cartItem">

        <button id="productButtonOne" class="cartItems">Add to cart</button>
        <img src="monaLisa.jpg" id="monaLisaImage">
        <h2 class="productDescriptions">Mona Lisa</h2>
        <p>This painting is great.</p>
        <span class=price>This item costs $10.</span>

        </div>




标签: javascriptnulltypeerroraddeventlistener

解决方案


将脚本标签放在正文之后它会起作用

<!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>


推荐阅读