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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這段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 關注
  • 1324 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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