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

时间:2015-10-01 12:43:45

标签: javascript html if-statement hide show

我正在为学校制作一个网站,但我的问题仍然存在于我的javascript问题上。我是javascript的新手,所以它可能非常简单,所以请幽默我。

在css中我将显示设置为" none"对于具有id navigatie的元素。 如果我删除了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>

4 个答案:

答案 0 :(得分:3)

您使用的是等号运算符,而不是比较运算符。使用==代替=

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

这应该有效

答案 1 :(得分:0)

您只需按以下方式更改if条件:

if(nav.style.display = "none"){
...Your Code
}

这应该有效:

if(nav.style.display == "none"){
    ...Your Code
    }

答案 2 :(得分:0)

你可以试试这个:

 if(nav.style.display == "none"){
  ...Enter Your Code
}

答案 3 :(得分:0)

尝试将您的情况更改为以下内容:

if(nav.style.visibility !== 'hidden')