First, I have seen many articles on the Internet divided into two (incorrect) points of view:
1. cancelBubble is used for ie to prevent bubbling events, and event. stopPropagation () is used for firefox, chrome, and other browsers.
Do not mention whether the above is correct or not
Let's take a look at an example: (test environment: chrom5.0.275.7, ipv3.6.4, opera10.53, ie6, 7,8)
Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
</Head>
<Body onclick = "alert ('body');">
<Input id = "Button1" type = "button" value = "button" onclick = "clickBtn (event)"/>
<Script language = "javascript" type = "text/javascript">
Function clickBtn (event)
{
Event = event? Event: window. event;
Event. cancelBubble = true;
Alert (event. cancelBubble );
}
</Script>
</Body>
</Html>
Tested:
A, chrom5.0.275.7, opera10.53, ie6, 7, 8 in these browsers, cancelBubble is valid and can prevent event bubbles, so that the onclick of the body cannot be triggered. Only the click of the button is triggered.
Alert (event. cancelBubble); the output result is true.
B. In release 3.6.4, onclick of the body cannot be blocked, but alert (event. cancelBubble); the output result is still true. I think this should be an event. cancelBubble only adds an attribute to the event and assigns the value true;
When the js Code is changed to this:Copy codeThe Code is as follows: <script language = "javascript" type = "text/javascript">
Function clickBtn (event)
{
Event = event? Event: window. event;
Event. stopPropagation ();
Alert ("123 ");
}
</Script>
Can be effectively blocked. Of course, event. stopPropagation (); In chrome and opera is also valid,
Conclusion 1: The event is described above. cancelBubble is supported in the new version of chrome and operabrowser, so it is worse than moz. In fact, I personally think event. cancelBubble is better than event. stopPropagation () is better, not only in English. Therefore, we hope that moz can release a new version. This makes it compatible.
2. There are also frequent issues about the event sequence:
Incomplete and accurate conclusion (self-think)
Ie: Source event element> parent element event> body --> document
Moz: the opposite of other browsers.
Let's take a look at an example:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
</Head>
<Body onclick = "alert ('body');">
<Div onclick = "clickBtn (event)" style = "width: 100px; height: 100px; background: #666;">
<Input id = "Button1" type = "button" value = "button" onclick = "alert ('btn ');"/>
</Div>
<Script language = "javascript" type = "text/javascript">
Function clickBtn (event)
{
Event = event? Event: window. event;
Event. stopPropagation ();
Alert ("123 ");
}
</Script>
</Body>
</Html>
According to the above view, if I click the button event from body ----> div -----> button, then alertbody is triggered first, and then the div is popped up with 123, because the bubble is blocked, therefore, the click of a button is not triggered.
But we have tested it. Moz is triggered from the inside out. Correctly execute alert ("btn") ---> alert ("123") ---- prevent the click Event of the body from being triggered by bubbling
At this point, do you doubt that the above is incorrect, but the above method is correct for the events added with addListenter and attachEvent (). For example:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
</Head>
<Body>
<Ul id = 'ul '>
<Li id = 'li1'> List Item1 </li>
<Li id = 'li2'> List Item2 </li>
</Ul>
<Script language = "javascript" type = "text/javascript">
Function init (){
If (!! Document. all ){
Document. getElementById ('li1'). attachEvent ('onclick', function (event ){
Alert ('li1 ');
})
Document. getElementById ('li2'). attachEvent ('onclick', function (event ){
Alert ('li2 ');
})
Document. getElementById ('ul '). attachEvent ('onclick', function (event ){
Alert ('ul ');
// Event. cancelBubble = true;
Alert (event. stopPropagation );
});
} Else {
Document. getElementById ('li1'). addEventListener ('click', function (event ){
Alert ('li1 ');
}, True)
Document. getElementById ('li2'). addEventListener ('click', function (event ){
Alert ('li2 ');
}, True)
Document. getElementById ('ul '). addEventListener ('click', function (event ){
Event = event? Event: window. event;
Event. stopPropagation ();
Alert ('ul ');
}, True );
}
}
Init ();
</Script>
</Body>
</Html>
This method is applicable. The execution result is to trigger the ul click event, and the click event is not triggered when you click li because the bubble is blocked. (By The Way, using this method to dynamically add events seems like event. cancelBubble is also valid in moz, but there is a difference between chrome and moz)