JS 獲取觸發事件的對象


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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM