javascript无法正常工作,我有什么遗漏吗?

时间:2019-12-29 16:28:58

标签: responsive-design

我一直在关注Devslopes教程,以使导航栏具有响应性,但我遇到了困难。在他的视频中,编写了javascript代码和mediaquery CSS之后,他的导航栏显示了,但我的导航栏却没有显示,而且我做的完全一样。

tutorial video link

html代码:

<html lang="en">
<head>
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <nav>
        <ul class="topnav" id="dropdownClick">
            <li><a href="#home">Home</a></li>
            <li><a href="#news">News</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#about">About</a></li>
            <li class="topnav-right"><a href="#signup">Sign-Up</a></li>
            <li class="topnav-right"><a href="#signin">Sign-In</a></li>
            <li class="dropdownIcon"><a href="javascript:void(0);"  onclick="dropdownMenu()">&#9776;</a></li>
        </ul>
    </nav>  

    <script>
        function dropdownMenu(){
            var x = document.getElementById(dropdownClick);
            if(x.className === "topnav"){
                x.className += " responsive";
                // change topnav to topnav.responsive
            } else{
                x.className = "topnav";
            }
        }
    </script>

</body>
</html>

Css代码:

/* *****   defaults   ******** */
nav,
header,
footer{
    display: block;
}

body{
    line-height: 1;
    margin: 0;
    padding: 0;
}

/* ******   Navbar   ***********/

nav{
    width: 100%;
    margin: 0;
}

nav ul{
    background-color: #eee;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

ul.topnav li{
    list-style: none;
    float: left;
}

ul.topnav li.topnav-right{
    float: right;

}

ul.topnav li a{
    display: block;
    text-decoration: none;
    min-height: 16px;
    text-align: center;
    padding: 14px;
    text-transform: uppercase;
    color: #666;
}

ul.topnav li a:hover{
    background-color: #0088ff;
    color: #fff;
}

ul.topnav li.dropdownIcon{
    display: none;

}

/* ************  mobile  ******************** */
@media screen and (max-width: 680px){
    ul.topnav li:not(:nth-child(1)){
        display: none;
    }

    ul.topnav li.dropdownIcon{
        display: block;
        float: right;
    }

    ul.topnav.responsive{
        position: relative;
    }

    ul.topnav.responsive li{
        display: inline;
        float: none;
    }

    ul.topnav.responsive li a{
        display: block;
        text-align: left;
    }
}


Thank you.

1 个答案:

答案 0 :(得分:0)

var x = document.getElementById(dropdownClick);中,元素ID(dropdownClick)是一个字符串,应使用诸如var x = document.getElementById('dropdownClick');的引号