Document directory
- 1. The mouseover event is also triggered when you move the cursor inside div [id = parent1 ].
- 2. The mouseenter event is not triggered when you move the cursor inside div [id = parent2 ].
First, let's take a look at several event object attributes.
TargetIndicates the event source object. Click an element at the innermost layer of the nested element. The element is the target. In IE6/7/8, srcElement is used.
CurrentTargetThe element that adds the event handler. For example, el in el. addEventListener is currentTarget. IE6/7/8 does not have a corresponding attribute. You can use this in handler to replace evt. currentTarget = this.
RelativeTargetEvent-related elements, which are generally used in mouseover and mouseout events. In IE6/7/8, it corresponds to fromElement and toElement.
Mouseenter and mouseleave IE9 are still supported. For more information, see Goodbye mouseover and hello mouseenter.
The difference between mouseenter and mouseover is that mouseenter is not triggered when the element is moved internally. As follows:
Copy codeThe Code is as follows: <! Doctype html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Differences Between mouseerter and mouseover (test in IE) </title>
</Head>
<Body>
<Div id = "result" style = "position: absolute; right: 100px; top: 5px; width: 250px; height: 400px; border: 2px solid gray; overflow: auto; ">
</Div>
<H3> 1. When you move the cursor inside div [id = parent1], the mouseover event is also triggered. <Div id = "parent1" style = "width: 400px; border: 1px solid gray; padding: 5px;">
<Div id = "child11" style = "width: 100px; height: 100px; background: gold; float: left;"> Child11 </div>
<Div id = "child12" style = "width: 100px; height: 100px; background: gold; float: right;"> Child12 </div>
<Div style = "clear: both;"> </div>
</Div>
<Br/>
<H3> 2. If you move the cursor inside div [id = parent2], the mouseenter event is not triggered. <Div id = "parent2" style = "width: 400px; border: 1px solid gray; padding: 5px;">
<Div id = "child21" style = "width: 100px; height: 100px; background: gold; float: left;"> Child21 </div>
<Div id = "child22" style = "width: 100px; height: 100px; background: gold; float: right;"> Child22 </div>
<Div style = "clear: both;"> </div>
</Div>
<Script type = "text/javascript">
Function on (el, type, fn ){
El. addEventListener? El. addEventListener (type, fn, false): el. attachEvent ('on' + type, fn );
}
Function $ (id ){
Return document. getElementById (id );
}
Var p1 = $('parent1 '),
P2 = $('parent2 ');
Function fn (e ){
Var d = document. createElement ('div ');
D. innerHTML = e. type;
$ ('Result'). appendChild (d );
}
On (p1, 'mouseover', fn );
On (p2, 'mouseenter', fn );
</Script>
<Body>
</Html>
After understanding the meaning of these three attributes, it is easy to implement:Copy codeThe Code is as follows: E = function (){
Function elContains (a, B ){
Try {
Return a. contains? A! = B & a. contains (B ):!! (A. compareDocumentPosition (B) & 16 );
} Catch (e ){}
}
Function addEvt (el, type, fn ){
Function fun (e ){
Var a = e. currentTarget, B = e. relatedTarget;
If (! ElContains (a, B) &! = B ){
Fn. call (e. currentTarget, e );
}
}
If (el. addEventListener ){
If (type = 'mouseenter '){
El. addEventListener ('mouseover', fun, false );
} Else if (type = 'mouseleave '){
El. addEventListener ('mouseout', fun, false );
} Else {
El. addEventListener (type, fn, false );
}
} Else {
El. attachEvent ('on' + type, fn );
}
}
Return {addEvt: addEvt };
}();
Test code:<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> 1, when you move the mouse inside div [id = parent1], the mouseover event Child11Child122 is also triggered. If you move the mouse inside div [id = parent2], the mouseenter event Child21Child22 is not triggered. <p>