亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

老師我在 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)

??????})

????});

??}



}



正在回答

1 回答

你發的代碼錯誤百出,核對一下代碼吧。

報錯是發生在倒數第二個remove那里的,第一個循環的事件是mouseenter不是mouse,而且你第二個循環那里怎么有兩個循環?而且怎么是item.classList?

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

老師我在 item.addEventListener中的 li.children[1].classList.remove("active");一直報錯說 Cannot read property 'classList' of undefined

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號