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

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

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

跃然一笑 2018-07-15 13:26:39
想写个循环测试一下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); } }错误提示:开发工具(idea)提示:mutable variable is accessible from closure浏览器报错提示:可以正确执行的代码: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 回答
  • 1 关注
  • 3337 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号