首页 > 解决方案 > why my last.addEventListener("click",nextLevel) not working?

问题描述

https://codepen.io/demi-chen/pen/RwGPgxv

 let last = theLeftSide.lastElementChild

 last.addEventListener("click",nextLevel)
 console.log(last) =====> it does show <img>......why the addEventListener not working 

 function nextLevel(){

 event.stopPropagation();
 numberOfFaces += 5;
 generateFaces();

 }

I try to find the lastElementChild in the first div. It does show ...... after I use console.log to check. why the addEventListener not working. I click the lastElementChild smile.png face but it's not working. if function nextLevel() is working. the left&right side should add more smile.png.

Tks!

标签: javascriptfunctiondomdom-eventsaddeventlistener

解决方案


its quite a simple fix really change your nextlevel to

function nextLevel(event){
  event.stopPropagation();
  numberOfFaces += 5;
  generateFaces();
}

推荐阅读