6条回答
  •  不思量自难忘°
    2021-01-21 21:13

    Thanks to the new JS standards this can be done easily. All my a tags had the class nav-link. So I selected them all (which gave me a node list) and ran forEach over them all and added a function to call whenever one of the nodes were selected. Hope this helps!

    let navLink = document.querySelectorAll('.nav-link');
    const navbar = document.querySelector('#navbarNav');
    
    closeNavBar = () => {
        navbar.classList.remove('show');
    };
    
    navLink.forEach((nav) => nav.addEventListener('click', closeNavBar));
    

提交回复
热议问题