跟着老师打代码,游览器提示‘addEventListener’为空
(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