HTML DOM MouseEvent relatedTarget
HTML DOM MouseEvent relatedTarget 属性返回触发特定鼠标悬停或鼠标移出事件的相应元素。
以下是语法 -
返回对 relatedTarget 对象的引用
MouseEventObject.relatedTarget
让我们来看一个 MouseEvent relatedTarget 属性的示例 -
示例
<!DOCTYPE html> <html> <head> <title>MouseEvent relatedTarget</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } #outer { width:70%; margin: 0 auto; padding: 0; text-align: center; border:1px solid black; height: 105px; background-color: #28a745; } input[type="button"] { border-radius: 10px; } #upper { border-bottom: 1px solid black; height: 40px; margin: 0 0 15px 0; background-color: #DC3545; } #lower { border-top: 1px solid black; height: 40px; margin: 15px 0 0 0; background-color: #DC3545; } </style> </head> <body> <form> <fieldset> <legend>MouseEvent-relatedTarget</legend> <div id="outer" onmouseout="gameStart(event)"> <div id="upper"><h2>Danger</h2></div> <div id="lower"><h2>Danger</h2></div> </div> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var textSelect = document.getElementById("textSelect"); function gameStart(event) { var fetchedID = event.relatedTarget.id if(fetchedID !== '') divDisplay.textContent = 'You are hovering over '+fetchedID+' <div> element'; } </script> </body> </html>
输出
悬停在绿色(安全)区域 -
悬停在上部红色(危险)区域 -
悬停在下部红色(危险)区域 -
广告