Prevent bubbles:
1. stopPropagation () for non-ie browsers.
2. The cancelBubble attribute is true. For ie browsers,
Jquery already has browser-compatible methods, such as event. stopImmediatePropagation ();
Copy codeThe Code is as follows:
<Style>
Body
{
Background: black;
}
# MyDiv
{
Background: # fff;
Width: 250px;
Height: 250px;
Display: none;
}
</Style>
<Div id = "myDiv">
This is a div;
</Div>
<Input id = "btn" type = "button" value = "show DIV"/>
<Script type = "text/javascript">
Var myDiv =$ ("# myDiv ");
$ (Function (){
$ ("# Btn"). click (function (event ){
ShowDiv (); // call the display DIV Method
$ (Document). one ("click", function () {// bind a shadow Div Method to the document
$ (MyDiv). hide ();
});
Event. stopPropagation (); // prevents events from bubbling up
});
$ (MyDiv). click (function (event ){
Event. stopPropagation (); // prevents events from bubbling up
});
});
Function showDiv (){
$ (MyDiv). fadeIn ();
}
</Script>