单击时,我试图将一个类添加到锚标记。但是,当我尝试使用e.classList.add('active-link');I'm trying to use the same method being used here时,我得到“添加”未定义。
const links = document.querySelectorAll('#navbar__list li a');
links.forEach(function(e) {
// e.addEventListener('scroll', function() {
// e.preventDefault();
// for (let i = 0; i < links.length; i++) {
// }
// });
e.addEventListener('click', function(e) {
// first remove active_link and active-section class from all a and section elements
links.forEach(function(e) {
e.classList.remove('active-link');
//console.log(`#section${e.id}`);
//document.querySelector(`#section${e.id}`).classList.remove('active-section');
})
// add the active-class to the a element and active-section to the linked section
e.classList.add('active-link');
//document.querySelector(`#section${e.id}`).classList.add('active-section');
})
});
潇潇雨雨
红糖糍粑
相关分类