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 onmouseout="mouseoutHandler(event)" id="div1" style=" width:50px; height:50px;">div1</div>
- <div id="div2" style="width:50px; heigth:50px;" >div2</div>
本文深入探讨了在JavaScript中处理事件时使用的关键属性,包括event.srcElement、event.fromElement和event.toElement。通过实例演示了如何在不同的事件类型(如onmouseover和onmouseout)下利用这些属性来获取事件触发和移动的目标对象。

被折叠的 条评论
为什么被折叠?



