2 回答
TA贡献1833条经验 获得超4个赞
您遇到问题是因为 e.addEventListener('click', function(e) ) e 是事件对象,您需要替换它:
e.classList.add('active-link');
```
By this
```
e.target.classList.add('active-link');
```
TA贡献1815条经验 获得超6个赞
您的e变量名称令人困惑:根据范围,它可能具有三个不同的值。尝试这个 :
const links = document.querySelectorAll('#navbar__list li a');
links.forEach(function(link) {
link.addEventListener('click', function(event) {
// first remove active_link and active-section class from all a and section elements
links.forEach(function(l) {
l.classList.remove('active-link');
})
// add the active-class to the a element and active-section to the linked section
link.classList.add('active-link');
})
});
添加回答
举报