Javascript attach an onclick event to all links

后端 未结 7 1430
青春惊慌失措
青春惊慌失措 2020-12-03 00:12

I want to attach a function on every link on the site to change a parameter.

How can I do this without jQuery?

How do I traverse every link (it might be a DO

相关标签:
7条回答
  • 2020-12-03 00:46

    better way:

    const item = document.querySelectorAll(".nav__item");
    
    item.forEach(link => {
      link.addEventListener("click", function () {
        link.classList.add("nav__item--active");
      });
    });
    
    0 讨论(0)
提交回复
热议问题