简体   繁体   English

使用JS if语句通过按钮更改div类(我在做什么错)?

[英]Using JS if statement to change div class with button (what am I doing wrong)?

 function navtoggle(elementRef) { if (document.getElementById('nav').className = 'closed') { document.getElementById('nav').className = 'open' } else if (document.getElementById('nav').className = 'open') { document.getElementById('nav').className = 'closed' }; } 
 .closed { display: none; } .open { display: block; } #navget { width: 100px; height: 100px; box-shadow: 0px 0px 15px black; } 
 <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> <title>HTML Library</title> </head> <body> <div id="nav" class="closed"> <ul> <li><a href="index.html"><h1>HOME</h1></a> </li> </ul> </div> <button onclick="JavaScript: navtoggle(this);" id="navget">NAV</button> </body> </html> 

Here is the Html: 这是HTML:

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <title>HTML Library</title>
    </head>
    <body>
        <div id="nav" class="closed">
            <ul>
                <li><a href="index.html"><h1>HOME</h1></a></li>
            </ul>
        </div>
 <button onclick="JavaScript: navtoggle(this);" id="navget">NAV</button>
    </body>
</html>

The JS: JS:

function navtoggle(elementRef){
if (document.getElementById('nav').className = 'closed') 
    {document.getElementById('nav').className = 'open'}

else if (document.getElementById('nav').className = 'open')
    {document.getElementById('nav').className = 'closed'};

}

And the CSS: 和CSS:

.closed {
    display: none;
}

.open {
    display: block;
}

#navget {
    width: 100px; height: 100px;
}

I know that it would be easier to just use a checkbox, and I do know how to do that. 我知道只使用复选框会更容易,而且我确实知道该怎么做。 However, I want it to just be a button so that I can make it look better and such. 但是,我希望它只是一个按钮,这样我才能使其看起来更好。 I'm rather new to JS so please do not be too harsh for the dumb question. 我对JS很陌生,所以请不要对这个愚蠢的问题太苛刻。 Thank you. 谢谢。

in conditional statement you have use "=" instead of compare "==" 在条件语句中,您使用“ =”而不是“ ==“

 if (document.getElementById('nav').className == 'closed') {
    document.getElementById('nav').className = 'open'
  } else if (document.getElementById('nav').className == 'open') {
    document.getElementById('nav').className = 'closed'

you have several problems in your sintax I made you a plunker here so you can see all the details, but basically: 您的sintax有几个问题,我在这里为您提供了一个小工具因此您可以看到所有详细信息,但基本上是:

function navtoggle(elementRef){
  if (document.getElementById('nav').className === 'closed')  {
    document.getElementById('nav').className = 'open'
  }
  else if (document.getElementById('nav').className === 'open'){
    document.getElementById('nav').className = 'closed'
  } //You was using semicolon here and that wasn't parsing well
}

I don't know if miss to add the script in the header also but let me know if it worked 我不知道是否也想在标题中添加脚本,但请告诉我它是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM