event.srcElement ,event.fromElement,event.toElement

自然,我们都习惯了 IE,在 IE 中要在函数中获得各事件对象很容易,直接用 event、event.srcElemtn、event.fromElement、event.toElement 就行了。在 FireFox 中获得触发事件的元素可以用 event.target,但其他两个 fromElement 和 toElement 就要费些周折。

 
1、event.srcElement :当前事件的源
   var obj = event.srcElement ? event.srcElement : event.target; //FF只能识别event.target
 
2、event.fromElement、event.toElement
   
  1. <script language="javascript">  
  2.     //addEventListener是为一个事件添加一个监听,使用方法见http://cindylu520.iteye.com/admin/blogs/588652  
  3.     //此处if判断是否是火狐浏览器  
  4.   if(window.addEventListener) { FixPrototypeForGecko(); }    
  5.   
  6.   function  FixPrototypeForGecko()    
  7.   {    
  8.     //prototype属性允许你向一个对象添加属性和方法  
  9.     //__defineGetter__和__defineSetter__是Firefox的特有方法,可以利用来它自定义对象的方法。  
  10.     //使用方法见:http://cindylu520.iteye.com/admin/blogs/588667  
  11.     //runtimeStyle   运行时的样式!如果与style的属性重叠,将覆盖style的属性!  
  12.       HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle);    
  13.       //代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态。  
  14.       window.constructor.prototype.__defineGetter__("event",window_prototype_get_event);    
  15.       //event.srcElement当前事件的源,IE下,event对象有srcElement属性,但是没有target属性;Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的  
  16.       Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);    
  17.       //当前事件有移动成分时,如onmouseover、onmouseout等fromElement、toElement表示移动事件的两个端点  
  18.       Event.prototype.__defineGetter__("fromElement",  element_prototype_get_fromElement);    
  19.       Event.prototype.__defineGetter__("toElement", element_prototype_get_toElement);  
  20.         
  21.   }    
  22.   
  23.   function  element_prototype_get_runtimeStyle() { return  this.style; }    
  24.   function  window_prototype_get_event() { return  SearchEvent(); }    
  25.   function  event_prototype_get_srcElement() { return  this.target; }    
  26.   
  27.   function element_prototype_get_fromElement() {    
  28.       var node;    
  29.       //relatedTarget 事件属性返回与事件的目标节点相关的节点。  
  30.       //对于 mouseover 事件来说,该属性是鼠标指针移到目标节点上时所离开的那个节点。  
  31.       //对于 mouseout 事件来说,该属性是离开目标时,鼠标指针进入的节点。  
  32.      //对于其他类型的事件来说,这个属性没有用。  
  33.      //详情:http://cindylu520.iteye.com/admin/blogs/588678  
  34.             if(this.type == "mouseover") node = this.relatedTarget;    
  35.       else if (this.type == "mouseout") node = this.target;    
  36.       if(!node) return;    
  37.       while (node.nodeType != 1)   
  38.           node = node.parentNode;    
  39.       return node;    
  40.   }  
  41.   
  42.   function  element_prototype_get_toElement() {    
  43.           var node;    
  44.           if(this.type == "mouseout")  node = this.relatedTarget;    
  45.           else if (this.type == "mouseover") node = this.target;    
  46.           if(!node) return;    
  47.           while (node.nodeType != 1)    
  48.              node = node.parentNode;    
  49.           return node;    
  50.   }  
  51.      
  52.   function  SearchEvent()    
  53.   {    
  54.       if(document.all) return  window.event;    
  55.          
  56.       func = SearchEvent.caller;    
  57.   
  58.       while(func!=null){    
  59.           var  arg0=func.arguments[0];    
  60.             
  61.           if(arg0 instanceof Event) {    
  62.               return  arg0;    
  63.           }    
  64.          func=func.caller;    
  65.       }    
  66.       return   null;    
  67.   }  
  68. </script>  

  

好了,现在不管是在 IE 中还是在 FireFox 中,触发事件后都有了 event、event.srcElement、event.fromElement、event.toElement 属性了。这就来做个测试吧:

Java代码  收藏代码
  1. <script>  
  2.   function test(){  
  3.       alert("event:" + event +", srcElement:"+event.srcElement.innerHTML+  
  4.         ", fromElement:"+event.fromElement.innerHTML + ", toElement:"+event.toElement.innerHTML)  
  5.   }  
  6. </script>  
  7.   
  8. <button onmouseout="test()">MouseOut</button><button onmouseover="test()">MouseOver</button>  

页面中有两个按钮 MouseOut 和 MouseOver,你掠过第一个按钮到第二个按钮上是,有看到这样内容的窗口:


从上图可以看出,其实我是在 Google 的 Chrome 浏览器中作的测试,也是有效的。标题虽说是兼容 IE 和 FireFox,但宽松点说就是 IE  和非 IE,因为 IE 总喜欢剑起偏锋,不按规范办事,不过这种事在 IE 8 中是收敛了许多。

原文地址:https://www.cnblogs.com/shirly77/p/6246576.html