Js implementation of the click div region hidden div region
- 2020-03-30 03:28:22
- OfStack
First, look at the JS event model. The JS event model bubbles upward. For example, after the onclick event is triggered by a DOM element, the event will follow the node upward until there is a click event bound to a parent node.
Stop bubbling: 1. StopPropagation () for non-internet explorer browsers. 2. CancelBubble property is true. For Internet explorer,
The Jquery has browser compatibility, the method of event. StopImmediatePropagation ();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/jquery-1.4.4.min.js" ></script>
<title></title>
</head>
<style type="text/css">
body
{
background-color:#999999;
}
#myDiv
{
background-color:#FFFFFF;
width:250px;
height:250px;
display:none;
}
</style>
<body>
<input id="btn" type="button" value=" According to DIV" />
<div id="myDiv">
This is a div;
</div>
</body>
<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();//Prevent events from bubbling up
});
$(myDiv).click(function (event)
{
event.stopPropagation();//Prevent events from bubbling up
});
});
function showDiv()
{
$(myDiv).fadeIn();
}
</script>