当我单击链接时,此功能就会启动。它需要删除具有属性 [data-route] 的元素上的所有“.is-active”类。并在与我单击的链接连接的 [data-route] 元素上添加“.is-active”类。
toggle: function(section){
var sections = document.querySelectorAll('[data-route]');
for (i = 0; i < sections.length; i++){
document.querySelector('[data-route]').classList.remove('is-active');
}
document.querySelector(section).classList.add('is-active');
}
但这行不通。它不会删除类(class)吗?
参见示例:http://jordypouw.github.io/myFED2/deeltoets1/index.html
附注它必须是原生 JavaScript。
最佳答案
toggle: function(section){
var sections = document.querySelectorAll('[data-route]');
for (i = 0; i < sections.length; i++){
sections[i].classList.remove('is-active');
// querySelectorAll return an array of dom elements, u can access them directly.
}
// I suppose in your case that ' section ' variable is the clicked element so :
section.classList.add('is-active')
// if not you have to store the dom element from the event, and add the class here.
}
关于javascript - 如何删除除您单击的类(class)之外的所有类(class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26003283/