如果将鼠标悬停在上面,您可以跟踪鼠标的哪个元素,如下所示:
<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"
or"blur"
事件上的当前目标,因为event.target
这些函数将指向鼠标刚刚离开的元素,而不是鼠标当前悬停的元素。
编辑:
我添加了一些代码,所以它只在输入没有焦点时记录。