Introduction to Mouseout and MouseOver event bubbling in Javascipt

Source: Internet
Author: User

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 ' &amp;&amp; e.type!= ' MouseOver ') return false;
var RELTG = E.relatedtarget? E.relatedtarget:e.type = ' mouseout '? E.toelement:e.fromelement;
while (RELTG &amp;&amp; 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.