var oBtns = document.querySelectorAll("button");
oBtns.forEach((item, index) => {
item.onmouseenter = () =>{
oBtns.forEach((temp,tempIndex) =>{
temp.className = index == tempIndex ? "current" : "";
})
}
})
var oBtns = document.querySelectorAll("button");
for(var i = 0;i < oBtns.length;i++){
oBtns[i].id = i;
oBtns[i].onmouseenter = function(){
for(var j = 0;j < oBtns.length;j++){
oBtns[j].className = j == this.id ? "current" : "";
}
}
}
var oBtns = document.querySelectorAll("button");
for(let i = 0;i < oBtns.length;i++){
oBtns[i].onmouseenter = () =>{
for(var j = 0;j < oBtns.length;j++){
oBtns[j].className = j == i ? "current" : "";
}
}
}
var oBtns = document.querySelectorAll("button");
for(var i = 0;i < oBtns.length;i++){
oBtns[i].onmouseenter = show(i)
}
function show(i){
return function(){
for(var j = 0;j < oBtns.length;j++){
oBtns[j].className = j == i ? "current" : "";
}
}
}
原生js解决排它问题(1.forEach 2.藏值 3.let 4.闭包)
于 2022-03-05 15:57:37 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)