0

私がこれを持っているとしましょう:

<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>

誰かがこれを実装する方法を知っていますか?

4

1 に答える 1

4

次のように、カーソルを合わせるとマウスの要素を追跡できます。

<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。これらの関数のは、マウスが現在ホバーしている要素ではなく、マウスがちょうど左にある要素を指すためです。

編集:
入力にフォーカスがない場合にのみログに記録されるように、いくつかのコードを追加しました。

于 2013-01-04T09:37:39.610 に答える