我想通过url来传递一个班级。目前,两个for循环总是被执行。有没有更简洁的写法以避免执行任何不必要的代码?
const arrCommon = ['foo1', 'foo2', 'foo3', 'foo4'];
const arrOther = ['bar1', 'bar2', 'bar3'];
const activeUrl = window.location.href;
const activePage = activeUrl.substring(activeUrl.lastIndexOf('/') + 1);
for(let i=0; i<arrCommon.length; i++) {
if (activePage == arrCommon[i])
//if 'foo1, foo2 foo3 or foo4' add class to element 1
}
for(let i=0; i<arrOther.length; i++) {
if (activePage == arrOther[i]) {
//if 'bar1' add class to element 2
//if 'bar2' add class to element 3
//...
}
}
[...arrCommon, ...arrOther]
将arrCommon
和arrOther
放入单个列表中。 - Michael Bauer学院活动,贡献,考试
- Fergoso