[英]How to check over what element my mouse is when I lose focus on input element
假設我有這個:
<body>
<input type="text" onblur="myFunction()" />
<... rest of page ...>
</body>
<script type="text/javascript">
function myFunction()
{
//this function alerts over what element your mouse is when the input loses focus
}
</script>
有人知道如何實現嗎?
您可以跟蹤鼠標懸停在哪個元素上,如下所示:
<input type="text" id="myInput" onblur="myFunction()" />
var input = document.getElementById('myInput'), // Get the input element
focus;
input.onmouseover = input.onfocus = function(){focus = true;}; // Set focus to true if the input gains focus
input.onmouseout = input.onblur = function(){focus = false}; // Set focus to false if the input loses focus
document.body.onmousemove = function(e){
if(!focus){ // Only log the target is the input doesn't have focus
console.log(e.target); //Log the current element the mouse is hovering over.
}
};
據我所知,沒有准確的方法來檢查"mouseleave"
或"blur"
事件上鼠標的當前目標,因為這些函數的event.target
將指向鼠標剛離開的元素,而不是指向鼠標當前正在懸停。
編輯:
我添加了一些代碼,因此僅在輸入沒有焦點時才記錄日志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.