Block JS event bubbling Pass (cancelbubble, stoppropagation)

Source: Internet
Author: User

<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Test.webform1"%>

<! DOCTYPE html>
<title> Block JS event bubbling Pass (cancelbubble, stoppropagation) </title>
<meta name= "keywords" content= "JS, event bubbling, cancelbubble,stoppropagation"/>
<script>
function dosomething (obj, evt) {
alert (obj.id);
var e = (evt)? Evt:window.event;
if (window.event) {
E.cancelbubble = true; Stop Bubbling Under IE
} else {
E.preventdefault ();
E.stoppropagation (); Block Bubbling under other browsers
}
}
</script>
<body>
First case:
<div id= "Parent1" onclick= "alert (this.id)" style= "Width:250px;background-color:yellow" >
<p>this is Parent1 div.</p>
<div id= "Child1" onclick= "alert (this.id)" style= "Width:200px;background-color:orange" >
<p>this is child1.</p>
</div>
<p>this is Parent1 div.</p>
</div>


<br/>

Second case:
<div id= "Parent2" onclick= "alert (this.id)" style= "Width:250px;background-color:cyan;" >
<p>this is Parent2 div.</p>
<div id= "child2" onclick= "dosomething (this,event);" style= "width:200px;background-color:lightblue;" >
<p>this is child2. Would bubble.</p>
</div>
<p>this is Parent2 div.</p>
</div>
</body>

Effect:

First case: Click on the Orange column, alert ("Child1"), Alert ("Parent1")

Click on the Yellow section, alert ("Parent1")

Second case: Click on the gray Light Section, alert ("Child2")

Click on the rest of the section, alert ("Parent2")

That is, in the second case, using the event.cancelbubble () =true prevents the bubbling sort.

-------------------------

<body>
<table border= "1" onclick= "alert (' table ');" >
<TR onclick= "alert (' tr1111 ');" >
<td> First </td>
</tr>
<TR onclick= "Test (event);" >
<td> haha </td>
</tr>
</table>
</body>
<script type= "Text/javascript" >
function test (event)
{
Alert ("haha tr");
Event.cancelbubble = true;
}
</script>

Bubble sort, when a child element is triggered, the event is passed to the parent element at a sub-element level, and the parent element also fires the event.

So if you use Event.cancelbubble=true to prevent the event from being passed up, the bubble sort is blocked.

Block JS event bubbling Pass (cancelbubble, stoppropagation)

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.