监听方法在js中的实现如下:
function addEventListener(string eventFlag, function eventFunc, [bool useCapture=false])
eventFlag : 事件名称,如click、mouseover…
eventFunc: 绑定到事件中执行的动作
useCapture: 指定是否绑定在捕获阶段,true为是,false为否,默认为true
在事件监听流中可以使用event.stopPropagation()来阻止事件继续往下流
IE中使用自有的attachEvent函数绑定时间,函数定义如下:
function attachEvent(string eventFlag, function eventFunc)
eventFlag: 事件名称,但要加上on,如onclick、onmouseover…
eventFunc: 绑定到事件中执行的动作
在事件监听流中可以使用window.event.cacenlBubble=false来阻止事件继续往下流
总结:addEventListener(string eventFlag, function eventFunc, [bool useCapture=false]),针对ff,chrome,safari浏览器,false指冒泡阶段,默认为true,指捕获阶段。不过一般我们都用false。
attachEvent(string eventFlag, function eventFunc),针对ie系列、还有opera浏览器,少了事件处理机制的参数,只指定事件类型(别忘了on)和触发哪个函数。
从 ucren-widgets 抽出来的代码
var onMethodExecute=function (o,n,f,beforeMethod){
var rad="__patch__"+Ucren.randomWord(16);
o[rad]=o[n];
delete o[n];
o[n]=function (){
var a=arguments;
beforeMethod&&f.apply(this,a);
this[rad].apply(this,a);
beforeMethod||f.apply(this,a);
};
};
51js那边抄的。你可以百度“有趣的方法监听实验”