为了账号安全,请及时绑定邮箱和手机立即绑定

JavaScript代码闭包的问题,错误提示:mutable variable is ......

JavaScript代码闭包的问题,错误提示:mutable variable is ......

智慧大石 2018-07-30 10:41:55
想写个循环测试一下onmouseout,onmouseover,下面是具体代码。下面这段代码不能正常执行var yy = document.getElementsByClassName("div_inTop_001"); for (var i = 0; i < yy.length; i++) { yy[i].addEventListener("mouseover", myfunction0000); yy[i].addEventListener("mouseout", myfunction0001); function myfunction0000(){     yy[i].style.backgroundColor="#A52A2A";     console.log(i); } function myfunction0001(){     yy[i].style.backgroundColor="#5B5B5B";     console.log(i); } } 可以正确执行的代码:var yy = document.getElementsByClassName("div_inTop_001"); for (var i = 0; i < yy.length; i++) { yy[i].addEventListener("mouseover", myfunction0000); yy[i].addEventListener("mouseout", myfunction0001); function myfunction0000(){     this.style.backgroundColor="#A52A2A";     console.log(i); } function myfunction0001(){     this.style.backgroundColor="#5B5B5B";     console.log(i); } } 我想知道为什么上面的那一段代码不能运行?下面那段代码为什么能运行 yy.length=5
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 600 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信