event.relatedTarget
在事件中涉及的其它任何DOM元素。
event.relatedTarget
对于mouseout
事件,它指向被进入的元素;对于mouseover
事件,它指向被离开的元素。
例子
对于锚点的 mouseout 事件,显示被进入的元素类型。
$("a").mouseout(function (event) { alert(event.relatedTarget.nodeName); // "DIV" });
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>event.relatedTarget</title> <style type="text/css"> #box{ width:150px; height:150px; background-color:blue; } #ant{ width:80px; height:50px; background-color:red; } </style> <script> window.onload=function(){ let ant=document.getElementById("ant"); ant.onmouseover=function(ev){ this.innerHTML=ev.relatedTarget.id; } } </script> </head> <body> <div id="box"> <div id="ant"></div> </div> </body> </html>