简体   繁体   English

使用 JavaSCript 将活动 class 动态添加到动态构建的导航菜单行项目

[英]Dynamically adding active class to a dynamically built nav menu line item using JavaSCript

I would like to dynamically add an 'active' class to the navigation menu link when it is clicked using addEventListener.我想在使用 addEventListener 单击导航菜单链接时将“活动” class 动态添加到导航菜单链接。 The nav menu is has also been added dynamically.导航菜单也已动态添加。 I need to do this with only JavaScript.我只需要使用 JavaScript 来执行此操作。 Thank you for any help.感谢您的任何帮助。

Here is what I have so far.这是我到目前为止所拥有的。 Not sure where to go from here不知道 go 从这里到哪里

 const navElements = document.querySelectorAll('section') const navBar = document.getElementById('navbar__list') navElements.forEach(function(section){ const navlistElement = `<li class='menu__link ${section.className}' data-link=${section.id}><a href="#${section.id}">${section.dataset.nav}</li>` navBar.insertAdjacentHTML('beforeend',navlistElement) }) const activeClasses = document.querySelectorAll('.menu__link'); for (x of activeClasses) { x.addEventListener('click', function(event) { event.preventDefault(); x.removeClass('active'); $(this).addClass('active'); }); }
 <nav class="navbar__menu"> <.-- Navigation starts as empty UL that will be populated with JS --> <ul id="navbar__list"></ul> </nav>... <section id="section1" data-nav="Section 1" class="your-active-class"> <div class="landing__container"> <h2>Section 1</h2> <p>Lorem ipsum dolor sit amet</p> </div> </section>...

The main reason it isn't working for you is that the elements don't exist in the document yet when you try to add the listeners.它对您不起作用的主要原因是,当您尝试添加侦听器时,文档中尚不存在元素。

There are two ways to fix this.有两种方法可以解决此问题。 One would be to add a listener to each <li> as you create them and before inserting them into the DOM.一种方法是在创建每个<li>时以及在将它们插入 DOM 之前为它们添加一个侦听器。 But the simpler solution is to use event delegation and attach one listener to the <ul> element and handle the event.target s.但更简单的解决方案是使用事件委托并将一个侦听器附加到<ul>元素并处理event.target

You have a number of syntax errors, but below is a working edit of your snippet.您有许多语法错误,但以下是您的代码段的有效编辑。

To control the classes assigned to an element you need to use the classList property of the element and its provided methods to add() , remove() or toggle() a class.要控制分配给元素的类,您需要使用元素的classList属性及其提供的方法来add()remove()toggle() class。

 const navBar = document.querySelector('.navbar__list'); const navElements = document.querySelectorAll('section'); navBar.addEventListener('click', function (event) { event.preventDefault(); navBar.querySelector('.active')?.classList.remove('active'); event.target.classList.add('active'); }); navElements.forEach(function (section) { const navlistElement = `<li class='menu__link ${section.className}' data-link=${section.id}><a href="#${section.id}">${section.dataset.nav}</li>` navBar.insertAdjacentHTML('beforeend', navlistElement) })
 .active { background-color: tomato; }
 <nav class="navbar__menu"> <!-- Navigation starts as empty UL that will be populated with JS --> <ul class="navbar__list"></ul> </nav> <section id="section1" data-nav="Section 1" class="your-active-class"> <div class="landing__container"> <h2>Section 1</h2> <p>Lorem ipsum dolor sit amet</p> </div> </section> <section id="section2" data-nav="Section 2" class="your-active-class"> <div class="landing__container"> <h2>Section 2</h2> <p>Lorem ipsum dolor sit amet</p> </div> </section>

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

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