老师我在 item.addEventListener中的 li.children[1].classList.remove("active");一直报错说 Cannot read property 'classList' of undefined
class Menu {
constructor(id) {
this.box = document.querySelector(id)
this.ul = this.box.querySelector("ul");
this.lis = this.box.querySelectorAll("li");//因为有多个li结点,所以加All
this.subMenu = this.box.querySelectorAll(".sub-menu");
this.time1 = null;
this.time2 = null;
this.init();//调用init
}
init() {
console.log("menu");
this.lis.forEach((item) => {
item.addEventListener("mouse", (e) => {
let li = e.target;
console.log("mouse");
this.time1=setTimeout(()=>
{
this.subMenu.forEach((item)=>
{
item.classList.remove("active");
})//在这里用循环
if(this.time1 !=null)
{
clearInterval(this.time1);
}
li.children[1].classList.add("active");//children[1]就是li下的第二个子类
},200)
})
});
//item.addEventListenneer代表鼠标触发事件,e有点像函数一样包含其中的事件
this.lis.forEach((item) => {
item.addEventListener("mouseleave", (e) => {
let li = e.target;
console.log("mouseleave");
this.time2=setTimeout(() => {
this.subMenu.forEach((item)=>
{
item.classList.remove("active");
})//在这里用循环
li.children[1].classList.remove("active");
},200)
})
});
}
}