javascript - 显示/隐藏 div 功能不起作用

标签 javascript html if-statement hide show

我正在为学校制作一个网站,但我的 javascript 遇到了问题。我是 javascript 新手,所以这可能是非常简单的事情,所以请幽默我。

在 css 中,我已将 id navigatie 的元素的显示设置为“none”。 如果我删除 else{} 部分,代码可以工作,但使用它就不行了。有人知道我做错了什么吗?

这是我的代码:

<!DOCTYPE html>
<!--gemaakt door Timo Vossen D01-->
<html>
    <head>
        <title>Straight Outta Compton</title>
        <link rel="stylesheet" type="text/css" href="sac.css">
        <meta name="keywords" content="Gerecht">
        <meta name="author" content="Timo Vossen">
        <meta name="description" content="Straight Outta Compton fanpage">
        <meta charset="UTF-8">
        <body onload="homeFunction()">
    </head>

    <script type="text/javascript">
        function showhide(){
            var nav = document.getElementById("navigatie");
            if(nav.style.display = "none"){
            nav.style.display = "block";
            }
            else{
            nav.style.display = "none";
            }
        }
    </script>

    <body>
        <div id="navigatie">
            <ul class="navigatiebar">
                <li class="knop"><a href="#">Home</a></li>
                <li class="knop"><a href="#">Movie</a></li>
                <li class="knop"><a href="#">Actors</a></li>
                <li class="knop"><a href="#">N.W.A</a></li>
                <li class="knop"><a href="#">Media</a></li>
            </ul>
        </div>

        <input type="checkbox" id="navigatieknop" onclick="showhide()" class="navigatieknop" />
        <label for="navigatieknop"></label>
        <div class="website">
            <div id="inhoud">
                <div id="beschijving">
                    <h1 class="text">Straight Outta Compton (2015)</h1><br>
                    <p class="text">In the mid-1980s, the streets of Compton, California, were some of the most dangerous in the country. When five young men translated their experiences growing up into brutally honest music that rebelled against abusive authority, they gave an explosive voice to a silenced generation. Following the meteoric rise and fall of N.W.A., Straight Outta Compton tells the astonishing story of how these youngsters revolutionized music and pop culture forever the moment they told the world the truth about life in the hood and ignited a cultural war.</p>
                </div>
                <div id="videos">
                    <iframe width="100%" height="100%" src="https://www.youtube.com/embed/rsbWEF1Sju0" frameborder="0" allowfullscreen></iframe>
                </div>
                <div id="quote">
                    <h1 class="text"><q>If we keep goin' we can take over the goddamn world!</q></h1><br>
                    <p class="text">-Dr. Dre</p>
                </div>
                <div id="producers">
                    <img src="img/universal.png" id="uni"></img>
                    <img src="img/legendary.png" id="leg"></img>
                </div>
                <!--<div id="footer">
                </div>-->
            </div>
        </div>
    </body>
</html>

最佳答案

您使用的是等于运算符而不是比较运算符。使用 == 而不是 =

 var nav = document.getElementById("navigatie");
        if(nav.style.display == "none"){
        nav.style.display = "block";
        }
        else{
        nav.style.display = "none";
        }

这应该有效

关于javascript - 显示/隐藏 div 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32887922/

相关文章:

javascript - Protractor 超时 - "Failed: Timed out waiting for asynchronous Angular tasks to finish after 11 seconds."

javascript - JS - 找到小于或等于给定界限的最大整数,它可以被某个数字整除

html - 更改 HTML 背景图像的不透明度

java - 检查类型是否为接口(interface)

vba - 并非所有日期都能被 VBA 识别

javascript - Reactjs 仪表板 - 具有一组参数的多个实例

javascript - 检测用户是否有静音选项卡?

javascript - 删除表行的代码无法正常工作 Javascript/HTML

javascript - IE 和 Chrome 上的 MooTools 问题

r - else {} 语句在 for 循环中被忽略