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

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 回复 有任何疑惑可以回复我~

你把所有的都给我,给我一部分我也没法调

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
提交
取消
侧边栏信息展示效果
  • 参与学习       33645    人
  • 解答问题       96    个

顶级大牛分享开发经验,学会实现侧边栏内容效果展示,让你迅速进阶

进入课程

this.el.addEventListener 问题

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信