(function(){
var Menubar=function(){
this.el=document.querySelector('#sidebar ul');
this.state='allClosed';
this.el.addEventListener('click',function(e){//提示‘addEventListener’为空
e.stopPropagation();
});
}
var Sidebar=function(eId,closeBarId){
this.state='opened';
this.el=document.getElementById(eId||'sidebar');
this.closeBarEl=document.getElementById('closeBarId'||'closeBar ');
var self=this;
this.menubar=new Menubar();
this.el.addEventListener('click',function(event){
if(event.target!==this.el){
self.triggerSwich();
}
});
};
Sidebar.prototype.close=function(){
console.log('关闭');
this.state='closed'
};
Sidebar.prototype.open=function(){
console.log('打开');
this.state='opened';
};
Sidebar.prototype.triggerSwich=function(){
if(this.state==='opened'){
this.close();
}else{
this.open();
}
};
var sidebar=new Sidebar();
})();
Uncaught TypeError: Cannot read property 'addEventListener' of null
打印一下 this.el 你就知道错在哪边了