jquery realization Click on the page elsewhere to hide the specified element

Source: Internet
Author: User

The code example is as follows: <! DOCTYPE html>
<meta charset= "Utf-8" >
<meta name= "Author" content= "http://www.softwhy.com/"/>
<title> Click on the page to hide div-ant tribe in other places </title>
<style type= "Text/css" >
*{
margin:0px;
padding:0px;
}
. main{
Cursor:pointer;
}
. list{
BORDER:1PX solid black;
Display:none;
}
. List li{
Cursor:pointer;
border:1px solid red;
List-style:none;
}
#noPopEvent {
width:100px;
height:100px;
Background-color:blue;
font-size:12px;
margin:200px;
Text-align:center;
line-height:100px;
}
</style>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (". Main"). Click (function (e) {
$ (". List"). Toggle ();
E.stoppropagation ();
})

$ (document). Click (function () {
$ (". List"). Hide ();
})

$ (". List Li"). Click (function () {
$ (". Main"). Text ($ (this). text ());
$ (". List"). Hide ();
})
$ ("#noPopEvent"). Click (function (e) {
E.stoppropagation ();
})
});
</script>
<body>
<div style= "width:200px;margin:20px;" >
<div class= "Main" > Ant tribe </div>
<div class= "List" >
<ul>
<li>div+css Tutorials </li>
<li>javascript Tutorials </li>
</ul>
</div>
</div>
<div id= "nopopevent" > Dot I can't </div>
</body>

In the above code, click Ant Tribe can appear level two menu, when clicking on the page will be hidden anywhere else this level two menu, but click on the blue section can not hide the level two menu, the following describes the implementation of this effect.
I. Principle of implementation:
The principle is simple, the blue area of the Click event to add a blocking bubble, the other places do not prevent bubbling, so when the click of the event can bubble to document, and then the two-level drop-down menu is hidden.
Two. Code comments:
1.$. Ready (function () {}) when the document structure loads the shore wall and then executes the code in the function.
2.$ (". Main"). Click (function (e) {}), when you click on the ant tribe to achieve the two-level drop-down menu to hide and display the switch.
3.$ (". List"). Toggle (), drop-down menu display and hidden toggle.
4.e.stoppropagation (), prevent events from bubbling, otherwise the bubble is document, then the level two menu is always hidden.
5.$ (document). Click (function () {}) to register the click Time to document so that clicking anywhere can bubble the event to document and then hide the level two menu unless the event bubbling effect is blocked.
6.$ (". List"). Hide (), hides the level two menu.
7.$ (". List Li"). Click on an item in the level two menu to write the current text to the body and then hide the level two menu.
8.$ (". Main"). Text ((this). Text ()), which will be written to the body of the clicked two-level menu.
9.$ (". List"). Hide (), hides the level two drop-down menu.
10.$ ("#noPopEvent"). Click (function (e) {}) to register the event handler for the blue block, but the event bubbling effect is blocked, so the level two menu is not hidden.

jquery realization Click on the page elsewhere to hide the specified element

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.