Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   JS не видит элемент (https://javascript.ru/forum/misc/75640-js-ne-vidit-ehlement.html)

рони 26.10.2018 13:51

всё должно работать
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Творчество для всех</title>
    <link rel="stylesheet" href="css/style.css">
    <style type="text/css">
      .responsive {
          display: flex;
           flex-direction: column;
      }

    </style>
    <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
</head>
<body>
    <header>
        <div class="logo">
            <a href="index.html"><img class="grafic_logo" src="images/logo.png" alt=""></a>
        </div>
        <nav>
            <div class="topnav" id="myTopnav">
                <a href="index.html">HOME</a>
                <a href="projects.html">PROJECTS</a>
                <a href="blog.html">BLOG</a>
                <a href="contact.html">CONTACT</a>
                <a href="about.html">ABOUT</a>
                <a href="services.html">SERVICES</a>
                <a href="location.html">LOCATION</a>
                <a id="menu" href="#" class="icon">&#9776;</a>
            </div>
        </nav>
    </header>





    <script>
       menu.onclick = function myFunction(event) {
        event.preventDefault();
        var x = document.getElementById("myTopnav");

        if (x.className === "topnav") {
            x.className += " responsive";
        } else{
            x.className = "topnav";
        }
    }

    </script>
</body>
</html>

Nexus 26.10.2018 13:53

Как document может быть необъявленным?

ITMEGAMASTER 26.10.2018 13:59

Цитата:

Сообщение от Nexus (Сообщение 497086)
В js "menu" на document.getElementById('menu') заменять не пробовали?

не получилось

рони 26.10.2018 14:01

Цитата:

Сообщение от ITMEGAMASTER
не получилось

если здесь, всё у вас работает, значит у вас другой код или ошибки в html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Творчество для всех</title>
    <link rel="stylesheet" href="css/style.css">
    <style type="text/css">
      .responsive {
          display: flex;
           flex-direction: column;
      }

    </style>
    <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
</head>
<body>
    <header>
        <div class="logo">
            <a href="index.html"><img class="grafic_logo" src="images/logo.png" alt=""></a>
        </div>
        <nav>
            <div class="topnav" id="myTopnav">
                <a href="index.html">HOME</a>
                <a href="projects.html">PROJECTS</a>
                <a href="blog.html">BLOG</a>
                <a href="contact.html">CONTACT</a>
                <a href="about.html">ABOUT</a>
                <a href="services.html">SERVICES</a>
                <a href="location.html">LOCATION</a>
                <a id="menu" href="#" class="icon">☰</a>
            </div>
        </nav>
    </header>





    <script>
       document.getElementById('menu').onclick = function myFunction(event) {
        event.preventDefault();
        var x = document.getElementById("myTopnav");

        if (x.className === "topnav") {
            x.className += " responsive";
        } else{
            x.className = "topnav";
        }
    }

    </script>
</body>
</html>

j0hnik 26.10.2018 18:38

Цитата:

Сообщение от Nexus (Сообщение 497090)
Как document может быть не объявленным?

в node.js только наверное


Часовой пояс GMT +3, время: 23:44.