现在的位置: 首页 > 综合 > 正文

修改Event原型,从现在开始支持火狐

2017年04月13日 ⁄ 综合 ⁄ 共 1179字 ⁄ 字号 评论关闭

 firefox的垃圾之处,太多了,其中以不支持window.event等最为恶心,
因为这是JavaScript开发人员最常用的东东..

通过修改火狐的Event原型,我们可以在火狐里面直接使用window.event

实现原理主要在 function.caller.arguments[0] 这个密秘中...
曾经在实验中,function.caller.arguments[0]有时为返回Event类型..
仔细测试后终于找到了规律...
这是一极大喜事, 因为以前我们为了兼容火狐的事件, 需要使用 雅虎UI库的5个JS文件...
而现在...完全不用再这样做了..

复制代码

  1.  
  2. <script> 
  3. /*firefox*/ 
  4. function __firefox(){ 
  5.     HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style); 
  6.     window.constructor.prototype.__defineGetter__("event", __window_event); 
  7.     Event.prototype.__defineGetter__("srcElement", __event_srcElement); 
  8. function __element_style(){ 
  9.     return this.style; 
  10. function __window_event(){ 
  11.     return __window_event_constructor(); 
  12. function __event_srcElement(){ 
  13.     return this.target; 
  14. function __window_event_constructor(){ 
  15.     if(document.all){ 
  16.         return window.event; 
  17.     } 
  18.     var _caller = __window_event_constructor.caller; 
  19.     while(_caller!=null){ 
  20.         var _argument = _caller.arguments[0]; 
  21.         if(_argument){ 
  22.             var _temp = _argument.constructor; 
  23.             if(_temp.toString().indexOf("Event")!=-1){ 
  24.                 return _argument; 
  25.             } 
  26.         } 
  27.         _caller = _caller.caller; 
  28.     } 
  29.     return null; 
  30. if(window.addEventListener){ 
  31.     __firefox(); 
  32. /*end firefox*/ 
  33. </script> 

抱歉!评论已关闭.