简体   繁体   English

使用JS从html元素中删除class

[英]Removing class from html element with JS

I have been struggling for 2 days now with removing an added class from an Element.我已经为从元素中删除添加的 class 而苦苦挣扎了 2 天。 Checked all over the web, but can not find the answer to the problem.查遍了web,也找不到问题的答案。

I can add a class to the navigation and show the submenu that works fine.我可以将 class 添加到导航并显示工作正常的子菜单。

Then I want to hide the menu again when a user clicks anywhere in the window, but as soon as I type the code to remove the class, it does not work at all.然后我想在用户单击 window 中的任何位置时再次隐藏菜单,但是一旦我键入代码以删除 class,它就根本不起作用。 I am using only Vanilla JS without to look at jquery to master JS first.我只使用 Vanilla JS 而没有先看 jquery 来掌握 JS。

Thank you so much in advance, couldn´t find any solution on the web!在此先感谢您,在网上找不到任何解决方案!

https://jsfiddle.net/dx6ofnvs/4/ https://jsfiddle.net/dx6ofnvs/4/

HTML HTML

 <nav> 
  <div class="nav__navigation">
    <ul>
      
      <li id="services__btn"> <a href="#"> Leistungen</a></li>
        <div class="services__dropdown__container">
          <ul id="services__dropdown" class="dropdown">
              <li> <a href="#"> Link one</a></li>
              <li> <a href="#"> Link two</a></li>
              <li> <a href="#"> link three</a></li>
              <li> <a href="#"> link four</a></li>
           
          </ul> 
        </div>
      
   
    
    </ul>
  </div>
  
  <div class="burger">
    <div class="line1"></div>
    <div class="line2"></div>
    <div class="line3"></div>
  </div>

</nav>

CSS CSS


const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav__navigation");
const navLinks = document.querySelectorAll(".nav__navigation li");
const servicesBtn = document.querySelector("#services__btn");
const servicesDropdown = document.querySelector("#services__dropdown");
const servicesDropdownContainer = document.querySelector("#services__dropdown__container")
const html = document.querySelector("html")

///// Open Mobile Menu

burger.addEventListener("click", (navSlide));

function navSlide() {
    nav.classList.toggle("nav-active");
}



///// Open Dropdown Desktop Width
 servicesBtn.addEventListener("click", (e) => {
    servicesDropdown.classList.add("dropdown--active");    
}) 



window.addEventListener("click", (e) => {      
    if(e.target != servicesDropdown) {

/// I think this code ist the problem that it removes the class immedeatly after shown

        servicesDropdown.classList.remove("dropdown--active")
        console.log('clicked');        
    }    
})


The target element in the window function is not the button, is the link. window function 中的目标元素不是按钮,是链接。 You should check for it instead.您应该检查它。 I modified your fiddle so you can check it我修改了你的小提琴所以你可以检查它

    <li id="services__btn"> <a class="button" href="#"> Leistungen</a></li>

...

window.addEventListener("click", (e) => {
    let button = document.querySelector(".button");
        if(e.target !== button && servicesDropdown.classList.contains("dropdown--active")) {
            servicesDropdown.classList.remove("dropdown--active")
            console.log('clicked');        
        }
        console.log('e', e.target);
})

https://jsfiddle.net/ft2om9ak/1/ https://jsfiddle.net/ft2om9ak/1/

I have been struggling for 2 days now with removing an added class from an Element.我已经为从元素中删除添加的 class 苦苦挣扎了 2 天。 Checked all over the web, but can not find the answer to the problem.查遍了web,却找不到问题的答案。

I can add a class to the navigation and show the submenu that works fine.我可以在导航中添加一个 class 并显示工作正常的子菜单。

Then I want to hide the menu again when a user clicks anywhere in the window, but as soon as I type the code to remove the class, it does not work at all.然后,当用户单击 window 中的任何位置时,我想再次隐藏菜单,但是一旦我键入删除 class 的代码,它就根本不起作用。 I am using only Vanilla JS without to look at jquery to master JS first.我只使用 Vanilla JS 没有先看 jquery 来掌握 JS。

Thank you so much in advance, couldn´t find any solution on the web!非常感谢您,在网上找不到任何解决方案!

https://jsfiddle.net/dx6ofnvs/4/ https://jsfiddle.net/dx6ofnvs/4/

HTML HTML

 <nav> 
  <div class="nav__navigation">
    <ul>
      
      <li id="services__btn"> <a href="#"> Leistungen</a></li>
        <div class="services__dropdown__container">
          <ul id="services__dropdown" class="dropdown">
              <li> <a href="#"> Link one</a></li>
              <li> <a href="#"> Link two</a></li>
              <li> <a href="#"> link three</a></li>
              <li> <a href="#"> link four</a></li>
           
          </ul> 
        </div>
      
   
    
    </ul>
  </div>
  
  <div class="burger">
    <div class="line1"></div>
    <div class="line2"></div>
    <div class="line3"></div>
  </div>

</nav>

CSS CSS


const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav__navigation");
const navLinks = document.querySelectorAll(".nav__navigation li");
const servicesBtn = document.querySelector("#services__btn");
const servicesDropdown = document.querySelector("#services__dropdown");
const servicesDropdownContainer = document.querySelector("#services__dropdown__container")
const html = document.querySelector("html")

///// Open Mobile Menu

burger.addEventListener("click", (navSlide));

function navSlide() {
    nav.classList.toggle("nav-active");
}



///// Open Dropdown Desktop Width
 servicesBtn.addEventListener("click", (e) => {
    servicesDropdown.classList.add("dropdown--active");    
}) 



window.addEventListener("click", (e) => {      
    if(e.target != servicesDropdown) {

/// I think this code ist the problem that it removes the class immedeatly after shown

        servicesDropdown.classList.remove("dropdown--active")
        console.log('clicked');        
    }    
})


There exists a (DOM) contains function for cases like this.对于这种情况,存在一个contains function 的 (DOM)。 Which you can use like this:你可以这样使用:

const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav__navigation");
const navLinks = document.querySelectorAll(".nav__navigation li");
const servicesBtn = document.querySelector("#services__btn");
const servicesDropdown = document.querySelector("#services__dropdown");
const servicesDropdownContainer = document.querySelector(".services__dropdown__container")
const html = document.querySelector("html")

///// Open Mobile Menu

burger.addEventListener("click", (navSlide));

function navSlide() {
    nav.classList.toggle("nav-active");
}



///// Open Dropdown Desktop Width
 servicesBtn.addEventListener("click", (e) => {
    servicesDropdown.classList.toggle("dropdown--active");    
}) 



document.addEventListener("click", (e) => {
    if(!servicesDropdownContainer.parentNode.contains(e.target) && servicesDropdown.classList.contains("dropdown--active")) {
        servicesDropdown.classList.remove("dropdown--active")
        console.log('clicked');        
    }    
})

Also, services__dropdown__container is a class and not an id.此外, services__dropdown__container是 class 而不是 id。

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

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