The MouseOver () method triggers the MouseOver event, or the function that runs when the MouseOver event occurs
The Mouseout () method triggers the Mouseout event, or a function that runs when the Mouseout event occurs.
First look at an example
| code is as follows |
copy code |
| function Showdiv () { $ (' j_catelist '). style.display= ' block '; } The Function Hidediv () { $ (' j_catelist '). style.display= ' None '; } HTML <div class= "show-cate" id= "J_showcate" >&NBSP;&NBSP;&NBSP;&NBSP; <span id= "desc" Onmouseover= "Showdiv ();" ></span> <ul class= "cate-list" id= "J_catelist" Onmouseout= "Hidediv ();" fdafdsafsa </UL> </div> |
Bubble handling for Mouseout and mouseover events does not prevent event bubbling like the Click event through Evt.stoppropagation () and Evt.cancelbubble=true
The following is a solution:
| The code is as follows |
Copy Code |
function Ismouseleaveorenter (e, handler) { if (e.type!= ' mouseout ' && e.type!= ' MouseOver ') return false; var RELTG = E.relatedtarget? E.relatedtarget:e.type = ' mouseout '? E.toelement:e.fromelement; while (RELTG && RELTG!= handler) RELTG = Reltg.parentnode; Return (RELTG!= handler); }
Use: Element.onmouseout = function (e) { if (Ismouseleaveorenter (e, this)) { ..//Operation function } }
|
Interpretation of functions: in standard browsers, the elements that trigger events can be obtained by E.relatedtarget for MouseOver and Mouseout
Mouseout event-triggering elements in IE browsers can be e.toelement, and mouseover event-triggering elements can be obtained by e.fromelement
Determines whether the element that triggers the event is a child of the current element, and if so, does not execute the event, which solves the bubbling problem of mouseout and mouseover
JS Block event bubbling
| The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" > <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "> <title>js to prevent event bubbling demo</title> <script type= "Text/javascript" > Ways to block Bubbles function Stoppp (e) { var evt = e | | window.event; ie use CANCELBUBBLE=TRUE to block while FF under the need to use Stoppropagation method Evt.stoppropagation? Evt.stoppropagation (): (Evt.cancelbubble=true); } </script> <body> <div style= "margin:150px 400px;width:700px; height:550px; Background-color: #878788; "onclick=" alert (' onclick event on the outermost Div '); " > <div style= "margin:100px; width:500px; height:300px; Background-color: #545444; "onclick=" STOPPP (Arguments[0]); alert (' OnClick event on the middle-tier div '); " > <div style= "margin:60px 100px; height:100px; width:300px; Background-color:red "onclick=" STOPPP (Arguments[0]); alert (' OnClick event on the most inner div '); " > </div> </div> </div>
</body> |