次のように、カーソルを合わせるとマウスの要素を追跡できます。
<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
。これらの関数のは、マウスが現在ホバーしている要素ではなく、マウスがちょうど左にある要素を指すためです。
編集:
入力にフォーカスがない場合にのみログに記録されるように、いくつかのコードを追加しました。