JS 获取触发事件的对象

mac2022-07-05  19

event.srcElement:引发事件的目标对象,常用于onclick事件。

event.fromElement:引发事件的对象源,常用于onmouseout和onmouseover事件。

event.toElement:引发事件后,鼠标移动到的目标源,常用于onmouseout和onmouseover事件。

 

onmouseover时,srcElement相当于toElement;

onmouseout时,srcElement相当于fromSrcElement

 

小示例(兼容FF浏览器):

 

<mce:script type="text/javascript"><!--function mouseoutHandler(oEvent,objTagName) {var oEvent = oEvent ? oEvent : window.event var oElem = oEvent.toElement ? oEvent.toElement : oEvent.relatedTarget; // 此做法是为了兼容FF浏览器 alert(oElem.id); // 当鼠标移出Div1时,会弹出提示框提示鼠标移动到的另一个元素的ID }  // --></mce:script>  <div οnmοuseοut="mouseoutHandler(event)" id="div1" style=" width:50px; height:50px;">div1</div>  <div id="div2" style="width:50px; heigth:50px;" >div2</div>

转载于:https://www.cnblogs.com/A-Song/archive/2012/01/20/2328074.html

最新回复(0)