卡在这个错误中:Uncaught TypeError: Cannot read property

我开始尝试在 VS Code 中编写某种东西,我想我可以尝试制作一个移动 GUI,但由于 JavaScript 中的一些错误,我无法打开菜单(从第 230 行开始,问题在行238):

未捕获的类型错误:无法读取 HTMLDivElement 处 null 的属性“classList”。<anonymous>

我知道发生了什么,但我不知道如何让它发挥作用。

这是我到目前为止编写的脚本:

const navSlide = () => {

    const burger = document.querySelector('.burger');

    const nav = document.querySelector('.nav_links');


    burger.addEventListener('click', () => {

        nav.classList.toggle('nav-active');

    });

}


navSlide();

<!DOCTYPE html>

<html>

    <head>

        <title>TESTE 2</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <header>

            <img class="logo" src="Logo.jpg" alt="logo" width="80px" height="45px"><img>

            <div class="name">

                <h4 class="name"> </h4>

            </div>

            <nav>

                <ul class="nav__links">

                    <li>

                        <a href="Projects.html">Proiecte</a>

                    </li>

                    <li>

                        <a href="About.html">Despre</a>

                    </li>

                    <li>

                        <a href="Pagini.html">Pagini</a>

                    </li>

                    <li>

                        <a href="test_2.html">Acasa</a>

                    </li>

                </ul>

                <div class="burger">

                    <div class="burger1"></div>

                    <div class="burger2"></div>

                    <div class="burger3"></div>

                </div>

            </nav>

            <a class="buton_nav" href="Contact.html">Contact</a>

        </header>

    </head>

    <body>


<p>Text to test selection colors</p>


<div class="div1">


    <a button class="Buton_YT" href="#" target="_blank">Buton YT</a>


</div>



</body>

</html>


牧羊人nacy
浏览 33回答 1
1回答

侃侃无极

<html>&nbsp; <head><title>TESTE 2</title><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><style>&nbsp; @import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");&nbsp; .Buton_YT {&nbsp; &nbsp; font-size: 34px;&nbsp; &nbsp; padding: 5px 10px;&nbsp; &nbsp; border: 3px solid #000000;&nbsp; &nbsp; border-radius: 5px;&nbsp; &nbsp; text-decoration: none;&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; color: #09ff00;&nbsp; &nbsp; transition-duration: 0.2s;&nbsp; &nbsp; background-color: none;&nbsp; &nbsp; width: 120px;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; cursor: none;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; top: 30px;&nbsp; &nbsp; font-family: "arial", cursive;&nbsp; }&nbsp; ::selection {&nbsp; &nbsp; color: white;&nbsp; &nbsp; background-color: rgb(60, 255, 0);&nbsp; }&nbsp; .Buton_YT:hover {&nbsp; &nbsp; color: #ffffff;&nbsp; &nbsp; background-color: #ff0000;&nbsp; &nbsp; box-shadow: 5px #000000;&nbsp; }&nbsp; .Buton_YT:active {&nbsp; &nbsp; transition-duration: 100ms;&nbsp; &nbsp; background-color: #ff0000;&nbsp; &nbsp; box-shadow: 0 5px rgb(20, 20, 20);&nbsp; &nbsp; transform: translateY(4px);&nbsp; }&nbsp; .div1 {&nbsp; &nbsp; width: 500;&nbsp; &nbsp; height: 100;&nbsp; &nbsp; color: #00ffff;&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; position: absolute;&nbsp; }&nbsp; * {&nbsp; &nbsp; box-sizing: border-box;&nbsp; &nbsp; margin: 0px;&nbsp; &nbsp; padding: 0px;&nbsp; &nbsp; background-color: #333333;&nbsp; }&nbsp; li,&nbsp; a,&nbsp; .Buton_YT {&nbsp; &nbsp; font-family: "Montserrat", sans-serif;&nbsp; &nbsp; font-weight: 500;&nbsp; &nbsp; font-size: 18px;&nbsp; &nbsp; color: #edf0f1;&nbsp; &nbsp; text-decoration: none;&nbsp; &nbsp; justify-content: end;&nbsp; }&nbsp; header {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; justify-content: space-between;&nbsp; &nbsp; align-items: center;&nbsp; &nbsp; padding: 3px 1%;&nbsp; &nbsp; border: black 2px solid;&nbsp; &nbsp; background-color: #202020;&nbsp; }&nbsp; nav {&nbsp; &nbsp; background-color: #202020;&nbsp; }&nbsp; .logo {&nbsp; &nbsp; cursor: not-allowed;&nbsp; &nbsp; margin-right: auto;&nbsp; }&nbsp; /*.name {&nbsp; cursor: not-allowed;&nbsp; margin-right: flex-end;}*/&nbsp; .nav__links {&nbsp; &nbsp; list-style: none;&nbsp; &nbsp; background-color: #202020;&nbsp; }&nbsp; .nav__links li {&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; padding: 0px 20px;&nbsp; &nbsp; background-color: #202020;&nbsp; }&nbsp; .nav__links li a {&nbsp; &nbsp; transition-delay: 500ms;&nbsp; &nbsp; transition: all 0.2s ease 0s;&nbsp; &nbsp; background-color: #202020;&nbsp; }&nbsp; .nav__links li a:hover {&nbsp; &nbsp; color: rgba(0, 128, 160, 0.6);&nbsp; }&nbsp; .nav__links li a:active {&nbsp; &nbsp; color: rgba(0, 136, 169, 0.1);&nbsp; &nbsp; transition: all 0.001s ease 0s;&nbsp; }&nbsp; .buton_nav {&nbsp; &nbsp; padding: 9px 20px;&nbsp; &nbsp; background-color: rgba(0, 136, 169, 1);&nbsp; &nbsp; border: none;&nbsp; &nbsp; border-radius: 50px;&nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; transition: all 0.3s;&nbsp; }&nbsp; .buton_nav:hover {&nbsp; &nbsp; background-color: rgba(0, 128, 160, 0.6);&nbsp; &nbsp; transition: all 0.1s ease 0s;&nbsp; }&nbsp; .buton_nav:active {&nbsp; &nbsp; background-color: rgba(0, 136, 169, 0.3);&nbsp; }&nbsp; .burger {&nbsp; &nbsp; display: none;&nbsp; &nbsp; cursor: pointer;&nbsp; }&nbsp; .burger div {&nbsp; &nbsp; width: 25px;&nbsp; &nbsp; height: 3px;&nbsp; &nbsp; background-color: rgb(255, 255, 255);&nbsp; &nbsp; margin: 5px;&nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; color: #202020;&nbsp; }&nbsp; @media screen and (max-width: 720px) {&nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; overflow-x: hidden;&nbsp; &nbsp; }&nbsp; &nbsp; .nav__links {&nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; right: 00px;&nbsp; &nbsp; &nbsp; height: 92vh;&nbsp; &nbsp; &nbsp; top: 54.5px;&nbsp; &nbsp; &nbsp; background-color: #202020;&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; flex-direction: column;&nbsp; &nbsp; &nbsp; align-items: center;&nbsp; &nbsp; &nbsp; width: 150px;&nbsp; &nbsp; &nbsp; transform: translateX(100%);&nbsp; &nbsp; &nbsp; line-height: 110px;&nbsp; &nbsp; &nbsp; transition: transform 0.5 ease-in;&nbsp; &nbsp; }&nbsp; &nbsp; .nav__links li {&nbsp; &nbsp; &nbsp; opacity: 30%;&nbsp; &nbsp; }&nbsp; &nbsp; .burger {&nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; &nbsp; background-color: #202020;&nbsp; &nbsp; }&nbsp; }&nbsp; .nav-active {&nbsp; &nbsp; transform: translateX(0px);&nbsp; }</style>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <header>&nbsp; <img&nbsp; &nbsp; class="logo"&nbsp; &nbsp; src="Logo.jpg"&nbsp; &nbsp; alt="logo"&nbsp; &nbsp; width="80px"&nbsp; &nbsp; height="45px"&nbsp; /><img />&nbsp; <div class="name">&nbsp; &nbsp; <h4 class="name"></h4>&nbsp; </div>&nbsp; <nav>&nbsp; &nbsp; <ul class="nav__links">&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="Projects.html">Proiecte</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="About.html">Despre</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="Pagini.html">Pagini</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="test_2.html">Acasa</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; </ul>&nbsp; &nbsp; <div class="burger">&nbsp; &nbsp; &nbsp; <div class="burger1"></div>&nbsp; &nbsp; &nbsp; <div class="burger2"></div>&nbsp; &nbsp; &nbsp; <div class="burger3"></div>&nbsp; &nbsp; </div>&nbsp; </nav>&nbsp; <a class="buton_nav" href="Contact.html">Contact</a>&nbsp; &nbsp; </header>&nbsp; &nbsp; <p>Text to test selection colors</p>&nbsp; &nbsp; <div class="div1">&nbsp; &nbsp; &nbsp; <a button class="Buton_YT" href="#" target="_blank">Buton YT</a>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; const navSlide = () => {&nbsp; &nbsp; &nbsp; &nbsp; const burger = document.querySelector(".burger");&nbsp; &nbsp; &nbsp; &nbsp; const nav = document.querySelector(".nav__links");&nbsp; &nbsp; &nbsp; &nbsp; burger.addEventListener("click", () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav.classList.toggle("nav-active");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; navSlide();&nbsp; &nbsp; </script>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript