[英]Add and remove a class to a collection of items on button click (vanilla JS)
[英]Click a nav item, add a class, remove class from other nav items - vanilla JS
单击该项目后,我就可以向其添加一个is-active
类。 但是,我想删除该类,并在单击另一个时将其添加到另一个导航项。
这是我目前正在使用的内容:
JS:
const links = document.querySelectorAll('a');
links.forEach(function(link, index){
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
this.classList.add('is-active');
}
});
});
这种尝试会添加该类,但是在单击另一个链接时不会将其删除。
我将如何删除课程? 提前致谢。
您只需遍历不是this
的链接:
const links = document.querySelectorAll('a');
links.forEach(function(link, index){
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
this.classList.add('is-active');
links.forEach(l => { // ***
if (l !== this) { // ***
l.classList.remove('is-active'); // ***
} // ***
});
}
});
});
(见下面的for-of
版本。)
或者,您可以仅对is-active
链接执行新查询:
document.querySelectorAll('a').forEach(function(link, index){
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
document.querySelectorAll('a.is-active').forEach(activeLink => { // ***
activeLink.classList.remove('is-active'); // ***
}); // ***
this.classList.add('is-active');
}
});
});
或者,如果您愿意,因为应该只有一个,所以请使用querySelector
:
document.querySelectorAll('a').forEach(function(link, index){
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
const activeLink = document.querySelector('a.is-active'); // **
if (activeLink) { // **
activeLink.classList.remove('is-active'); // **
} // **
this.classList.add('is-active');
}
});
});
旁注: querySelectorAll
中的NodeList
在某些浏览器中没有forEach
(它是最近才添加的)。 请参阅此答案以了解如何在缺少它时添加它,以及(在ES2015 +平台上)如何确保它也是可迭代的(这也是它的本意)。
如果可以依靠可迭代性,则for-of
是这些for-of
版本:
第一个示例for-of
版:
const links = document.querySelectorAll('a');
for (const link of links) {
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
this.classList.add('is-active');
for (const l of links) {
if (l !== this) {
l.classList.remove('is-active');
}
}
}
});
}
第二个示例for-of
版:
for (const link of document.querySelectorAll('a')) {
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
for (const activeLink of document.querySelectorAll('a.is-active')) {
activeLink.classList.remove('is-active');
}
this.classList.add('is-active');
}
});
}
第三:
for (const link of document.querySelectorAll('a')) {
link.addEventListener('click', function() {
if(this.classList.contains('is-active')) {
this.classList.remove('is-active');
} else {
const activeLink = document.querySelector('a.is-active'); // **
if (activeLink) { // **
activeLink.classList.remove('is-active'); // **
} // **
this.classList.add('is-active');
}
});
}
常见要求-您可以执行以下操作:
使用[...document.querySelectorAll('a')]
类的spread syntax
获取DOM元素的可迭代表示形式
使用forEach
循环通过链接
您可以使用classList.toggle
函数代替if-else条件
请参见下面的演示:
// get an iterable representation using the spread syntax const elements = [...document.querySelectorAll('a')]; elements.forEach(e => e.addEventListener('click', () => { // remove active class from all links elements.forEach(e => e.classList.remove('is-active')); // add active to the clicked link e.classList.toggle('is-active'); }));
.is-active { color: red; }
<a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a>
this
函数中的this
表示单击的元素。 每次单击时都应使用forEach
来隐藏所有元素。 然后显示所需的一个
const links = [...document.querySelectorAll('a')];
links.forEach(function(link, index){
link.addEventListener('click', function() {
let temp = this.classList.contains('is-active')
links.forEach(x => x.classList.remove('is-active'))
if(!temp) {
this.classList.add('is-active');
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.