You can use mouseover or mouseout to trigger an event to determine whether the mouse is in this area. However, the limitation of this method is that it is necessary to trigger mouseover or mouseout, And the mouseleave event can only be known if this problem has been studied today.
Method 1:
You can use mouseover or mouseout to trigger an event to determine whether the mouse is in this area. However, the limitation of this method is that the mouseover, or mouseout, mouseleave event must be triggered to be known.
The Code is as follows:
Function chkIn ()
{
P_1.innerText = "now you have moved your mouse to the layer! ";
P_1.style.font = "normal black ";
}
Function chkOut ()
{
P_1.innerText = "now you have moved the mouse out of the layer! ";
P_1.style.font = "bold red ";
}
The Code is as follows:
OnMouseOver = "chkIn ()" onMouseOut = "chkOut ()"> This is a DIV
Method 2:
The Code is as follows:
Function checkIn (e ){
Var x = window. event. clientX;
Var y = window. event. clientY;
Var str = '';
For (I = 0; I Var obj = document. body. children [I];
If (x> obj. offsetLeft
& X <(obj. offsetLeft + obj. clientWidth)
& Y> obj. offsetTop
& Y <(obj. offsetTop + obj. clientHeight )){
Str + =' <鼠标位于层 '+obj.id+ '范围之内> \ N ';
} Else {
Str + =' <鼠标位于层 '+obj.id+ '范围之外> \ N ';
}
}
Alert (str );
}
Document. onclick = checkIn
Method 3:
This method is the simplest and practical.
The Code is as follows:
If (myDiv. contains (window. event. srcElement ))
That is, if (myDiv. contains (the element object at the mouse position ))
The specific situation should be selected based on your own needs. I debugged method 3, but it was not used. Other methods, continue to study.