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

為了賬號安全,請及時綁定郵箱和手機立即綁定

this.el.addEventListener 問題

sideBar.js:8 Uncaught TypeError: Cannot read property 'addEventListener' of null

正在回答

5 回答

(function(){

??var?SideBar=function(eId,closeId){

???this.state="opened";

???this.el=document.getElementById(eId||'sideBar');

??

???this.closeBar=document.getElementById(closeId||'closeBar');

???var?self=this;

???this.el.addEventListener('click',function(event){

????if?(event.target?!==?self.el){

??????self.triggerSwitch();?????

????}?

???});

??};

??SideBar.prototype.triggerSwitch=function(){

???if?(this.state==="opened")?{

????this.close();?????

????

???}else{

????this.open();

????

???}

??};

??SideBar.prototype.close=function(){

???this.state="closed";

??};

??SideBar.prototype.open=function(){

???this.state=="opened";

??};

??

??var?sidebar=new?SideBar();

??

?})();

0 回復 有任何疑惑可以回復我~
#1

夕顏egao

你最好把你的代碼放在window.onload里面或者最后,有可能是在js加載在id這個dom前了,這個時候的eId是undefined,所以這個時候用addeventlistener為null
2017-02-21 回復 有任何疑惑可以回復我~

你把所有的都給我,給我一部分我也沒法調

0 回復 有任何疑惑可以回復我~

if (this.state==="opened") {this.close();}else{this.open();}};

方法要加括號?

0 回復 有任何疑惑可以回復我~
#1

言長寸短 提問者

這個不影響的
2016-08-28 回復 有任何疑惑可以回復我~
#2

言長寸短 提問者

問題依舊存在
2016-08-28 回復 有任何疑惑可以回復我~

見圖

0 回復 有任何疑惑可以回復我~

貼源碼

0 回復 有任何疑惑可以回復我~
#1

言長寸短 提問者

this.state="opened"; this.el=document.getElementById(eId||'sideBar'); alert(this.el); this.closeBar=document.getElementById(closeId||'closeBar'); var self=this; this.el.addEventListener('click',function(event){ if (event.target !== self.el){ self.triggerSwitch();
2016-08-26 回復 有任何疑惑可以回復我~
#2

言長寸短 提問者

SideBar.prototype.triggerSwitch=function(){ if (this.state==="opened") {this.close;}else{this.open;}}; SideBar.prototype.close=function(){this.state="closed";}; SideBar.prototype.open=function(){this.state=="opened";}; var sidebar=new SideBar();
2016-08-26 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
側邊欄信息展示效果
  • 參與學習       33648    人
  • 解答問題       101    個

頂級大牛分享開發經驗,學會實現側邊欄內容效果展示,讓你迅速進階

進入課程

this.el.addEventListener 問題

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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