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

这段JS代码里为啥不能这样写

这段JS代码里为啥不能这样写

慕田峪8701529 2017-11-30 22:23:17
function Tab(oParent,child) {  this.oParent= document.getElementById(oParent);  this.aInput = this.oParent.getElementsByTagName(child);  this.aDiv = this.oParent.getElementsByTagName('div'); }     Tab.prototype.init=function ()  { for (var i = 0 ; i<this.aInput.length;i++){ var that = this; this.aInput[i].index=i; this.aInput[i].onclick=function () { //为什么这里不能直接写成this.aInput[i].onclick=that.change(this) that.change(this); } } Tab.prototype.change=function (obj) { for(var j=0 ;j<this.aInput.length;j++){ this.aInput[j].className =''; this.aDiv[j].style.display='none'; } obj.className = 'active' this.aDiv[obj.index].style.display='block'; } } window.onload=function () { var tabSwitch = new Tab('div1','input'); tabSwitch.init('div1')  } </script>
查看完整描述

2 回答

?
牛奶老哥哥

TA贡献204条经验 获得超92个赞

你这么写就直接在定义的时候就执行了啊,而且这个元素的点击也不会有任何事件响应.

查看完整回答
反对 回复 2017-12-01
?
cxxyjsj

TA贡献119条经验 获得超22个赞

onclick属性需要的类型是一个function, 你那么写是一个方法调用。返回值是undefined。

查看完整回答
反对 回复 2017-12-01
  • 2 回答
  • 0 关注
  • 1264 浏览
慕课专栏
更多

添加回答

举报

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