zoukankan      html  css  js  c++  java
  • 创建兼容 IE/FireFox 的 event 及 event 的 srcElement、fromElement、toElement 属性

    if(window.addEventListener) { FixPrototypeForGecko(); }  
      
       function  FixPrototypeForGecko()
       {
           HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle);
           window.constructor.prototype.__defineGetter__("event",window_prototype_get_event);
           Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);
           Event.prototype.__defineGetter__("fromElement",  element_prototype_get_fromElement);
           Event.prototype.__defineGetter__("toElement", element_prototype_get_toElement);
       }  
      
       function  element_prototype_get_runtimeStyle() { return  this.style; }
       function  window_prototype_get_event() { return  SearchEvent(); }
       function  event_prototype_get_srcElement() { return  this.target; }  
      
       function element_prototype_get_fromElement() {
           var node;
           if(this.type == "mouseover") node = this.relatedTarget;
           else if (this.type == "mouseout") node = this.target;
           if(!node) return;
           while (node.nodeType != 1)
               node = node.parentNode;
           return node;
       }
      
       function  element_prototype_get_toElement() {
               var node;
               if(this.type == "mouseout")  node = this.relatedTarget;
               else if (this.type == "mouseover") node = this.target;
               if(!node) return;
               while (node.nodeType != 1)
                  node = node.parentNode;
               return node;
       }
      
       function  SearchEvent()
       {
           if(document.all) return  window.event;  
           func = SearchEvent.caller;  
           while(func!=null){
               var  arg0=func.arguments[0];  
      
               if(arg0 instanceof Event) {
                   return  arg0;
               }
              func=func.caller;
           }
           return   null;
       }

    <script>
      function test(){
          alert("event:" + event +", srcElement:"+event.srcElement.innerHTML+
            ", fromElement:"+event.fromElement.innerHTML + ", toElement:"+event.toElement.innerHTML)
      }
    </script>
     
    <button onmouseout="test()">MouseOut</button><button onmouseover="test()">MouseOver</button>
  • 相关阅读:
    软件构架实践_阅读笔记04(-11)
    软件构架实践_阅读笔记03(7-9)
    Tsinsen-A1488 : 魔法波【高斯消元+异或方程组】
    Tsinsen-1487:分配游戏【树状数组】
    Tsinsen-1486:树【Trie树 + 点分治】
    Splay初步【bzoj1503】
    Treap初步
    [BZOJ3207] 花神的嘲讽计划Ⅰ
    可持久化Trie树初步
    可持久化线段树初步
  • 原文地址:https://www.cnblogs.com/bailuobo/p/2886514.html
Copyright © 2011-2022 走看看