firefox的垃圾之处,太多了,其中以不支持window.event等最为恶心,
因为这是JavaScript开发人员最常用的东东..
通过修改火狐的Event原型,我们可以在火狐里面直接使用window.event
实现原理主要在 function.caller.arguments[0] 这个密秘中...
曾经在实验中,function.caller.arguments[0]有时为返回Event类型..
仔细测试后终于找到了规律...
这是一极大喜事, 因为以前我们为了兼容火狐的事件, 需要使用 雅虎UI库的5个JS文件...
而现在...完全不用再这样做了..
复制代码
- <script>
- /*firefox*/
- function __firefox(){
- HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
- window.constructor.prototype.__defineGetter__("event", __window_event);
- Event.prototype.__defineGetter__("srcElement", __event_srcElement);
- }
- function __element_style(){
- return this.style;
- }
- function __window_event(){
- return __window_event_constructor();
- }
- function __event_srcElement(){
- return this.target;
- }
- function __window_event_constructor(){
- if(document.all){
- return window.event;
- }
- var _caller = __window_event_constructor.caller;
- while(_caller!=null){
- var _argument = _caller.arguments[0];
- if(_argument){
- var _temp = _argument.constructor;
- if(_temp.toString().indexOf("Event")!=-1){
- return _argument;
- }
- }
- _caller = _caller.caller;
- }
- return null;
- }
- if(window.addEventListener){
- __firefox();
- }
- /*end firefox*/
- </script>