简体   繁体   English

带有“隐藏”类的纯 JS 显示/隐藏不起作用

[英]Pure JS show/hide with 'hidden' class doesn't work

So I'm trying to do a navbar that show/hide when the toggle icon is clicked.所以我试图做一个导航栏,当点击切换图标时显示/隐藏。 I already solved the issue by not using a hidden class(using an onclick which fire a simple function with an if statement that change the style.display of the element I'm trying to show/hide).我已经通过不使用隐藏类解决了这个问题(使用 onclick 使用 if 语句触发一个简单的函数,该语句更改我试图显示/隐藏的元素的 style.display)。 Yes, it works but I'm trying to understand Javascript (pure), so I really need to know why my first solution (hidden class) didn't worked out.是的,它有效,但我试图理解 Javascript(纯),所以我真的需要知道为什么我的第一个解决方案(隐藏类)没有成功。 Here's some code (HTML/CSS then my attempt in pure JS):这是一些代码(HTML/CSS 然后是我在纯 JS 中的尝试):

 var nav = document.querySelector('nav'); var transf = document.getElementById('toggle'); var btn = document.querySelector('.iconBtn').addEventListener('click', i => { if (nav.classList = 'hidden') { nav.classList.remove('hidden'); transf.checked = true; console.log('show'); } else { nav.classList.add('hidden'); transf.checked = false; console.log('hide'); } console.log(nav.classList); console.log(transf.checked); })
 .hidden { display: none; }
 <div class="navMenu"> <nav class="hidden"> <ul> <li><a href="#">About</a></li> <li><a href="#">Innovations</a></li> <li><a href="#">Group</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </div> <div class="iconBtn"> <div class="toggle"> <input type="checkbox" id="toggle"> <label class="bars" id="bar1" for="toggle"></label> <label class="bars" id="bar2" for="toggle"></label> <label class="bars" id="bar3" for="toggle"></label> </div> </div> </div>

So here's what I tried to do with the js part:所以这就是我试图用 js 部分做的事情:

  • gave each selector a var给每个选择器一个 var
  • eventlistener on the btn that trigger and if statement触发和 if 语句的 btn 上的事件侦听器
  • basic if statement基本的 if 语句
  • need transf (toggle) checked cause I styled the icon: when it's checked, it goes from a list icon to an X需要 transf (toggle) 检查因为我设计了图标的样式:当它被选中时,它从一个列表图标变成一个 X
  • then console.logs everywhere to try to understand what is happening.然后到处都是 console.logs 试图了解发生了什么。

I also tried to add a second CSS class (.not-hidden: display ='flex';) to switch from one to the other: nope.我还尝试添加第二个 CSS 类(.not-hidden: display ='flex';)以从一个切换到另一个:不。

I tried to change the if statement by a switch case: same issue than with the if sstatement.我试图通过 switch case 更改 if 语句:与 if 语句相同的问题。

Behaviour: First click,'hidden' class is removed, if console.log returns "show", class = '', and checked is true: perfectenschlag.行为:第一次点击,'hidden'类被移除,如果console.log返回“show”,class = '',check为true:perfectenschlag。 But then, when I click once again, class 'hidden' is not added (nor toggled, I tried) and console.log gives the same feedback as the first click, not changing anything.但是,当我再次单击时,不会添加“隐藏”类(也没有切换,我尝试过)并且 console.log 提供与第一次单击相同的反馈,没有更改任何内容。

tdlr: First click add the class (and shows the navbar) but the second click, while triggered, doesn't change the class. tdlr:首先单击添加类(并显示导航栏),但第二次单击时触发,不会更改类。 It appear that the if statement seems to only return the if, and never goes to the else, even when else is the only possible option.看起来 if 语句似乎只返回 if,而从不返回 else,即使 else 是唯一可能的选项。

Finally, I know there's a high probability that this chunk of code is utter crap, and as I said, I found another solution, but as I'm just starting with JS I think it is important to know why JS behave the way it does and that's why I come here to ask.最后,我知道这段代码很可能完全是垃圾,正如我所说,我找到了另一个解决方案,但由于我刚刚开始使用 JS,我认为了解 JS 的行为方式很重要这就是我来这里问的原因。 Also, I don't know how I'm gonna make a smooth transition on my "nav.style.display" so I'm not giving up on the hidden class way.另外,我不知道如何在“nav.style.display”上进行平滑过渡,所以我不会放弃隐藏类的方式。

I cleaned up the code, and simplified the class switching using .toggle().我清理了代码,并使用 .toggle() 简化了类切换。

It looks like it's working for me.看起来它对我有用。

 var nav = document.querySelector('nav'); var btn = document.querySelector('.iconBtn') btn.addEventListener( 'click', () => nav.classList.toggle("hidden") );
 .hidden { display: none; }
 <div class="navMenu"> <nav class="hidden"> <ul> <li><a href="#">About</a></li> <li><a href="#">Innovations</a></li> <li><a href="#">Group</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </div> <div class="iconBtn"> <div class="toggle"> <input type="checkbox" id="toggle" /> <label class="bars" id="bar1" for="toggle"></label> <label class="bars" id="bar2" for="toggle"></label> <label class="bars" id="bar3" for="toggle"></label> </div> </div>

if (nav.classList = 'hidden') is not how you check to see if the list contains that class. if (nav.classList = 'hidden')不是您检查列表是否包含该类的方式。 Instead, that tries to assign 'hidden' to the property classList .相反,它试图'hidden'分配给属性classList

To check if it already has it, use contains :要检查它是否已经拥有它,请使用contains

if (nav.classList.contains('hidden'))

 var nav = document.querySelector('nav'); var transf = document.getElementById('toggle'); var btn = document.querySelector('.iconBtn').addEventListener('click', i => { if (nav.classList.contains('hidden')) { nav.classList.remove('hidden'); transf.checked = true; console.log('show'); } else { nav.classList.add('hidden'); transf.checked = false; console.log('hide'); } console.log(nav.classList); console.log(transf.checked); })
 .hidden { display: none; }
 <div class="navMenu"> <nav class="hidden"> <ul> <li><a href="#">About</a></li> <li><a href="#">Innovations</a></li> <li><a href="#">Group</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </div> <div class="iconBtn"> <div class="toggle"> <input type="checkbox" id="toggle"> <label class="bars" id="bar1" for="toggle"></label> <label class="bars" id="bar2" for="toggle"></label> <label class="bars" id="bar3" for="toggle"></label> </div> </div> </div>

您不需要使用 classList 删除,而是使用 setAttribute 来更改您的隐藏属性。

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

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