事件处理方法
先看代码:
var myapplation = {
handleClick: function(event){
event.preventDefault();
event.stopPropagation();
this.showPopup(event.clientX,event.clientY);
},
showPopup: function(x,y){
var pop = document.getElementById('popup');
popup.style.left = x + 'px';
popup.style.top = y + 'px';
popup.className = 'reveal';
}
};
规则1:隔离应用逻辑
这样写法可以很好地把应用逻辑分开,每个方法只处理各自的事件,方便阅读和后期管理
股则2:不要分发事件对象
不要每个方法都直接传入event对象,将event作为参数并不能告诉你那些是有用的参数,是用来干什么,这样会很不明确
myapplation.showPopup(10,10);//这样写方法可以直接调用会方便很多
addListener(element,'click',function(event){
myapplation.handleClick(event);
})