未捕获的类型错误:无法在 JavaScript.js:2 处设置 null 的属性“onclick”

时间:2021-07-02 09:52:47

标签: javascript html typeerror

document.getElementById("searchbutton").onclick = function(){
    var input1 = document.getElementById("searchbar").value;
    console.log(input1);
}
<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
        <link rel="shortcut icon" type="image/png" href="imgs/logo.png">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
    </head>
    <body>
        <!--Imports-->
        <link rel="stylesheet" type="text/css" href="Style.css">
        <script src="JavaScript.js"></script>
        <!--code-->
        <div style="text-align: center;">
            <img src="imgs/logo.png" class="logo" alt="Image">
            <input type="search" placeholder="   search" id="searchbar">
            <button type="button" id="searchbutton">
                <p>Search</p>
            </button>
        </div>
</html>

我的错误是在 js 代码上:Uncaught TypeError: Cannot set property 'onclick' of null at JavaScript.js:2

0 个答案:

没有答案