JS Click on the page elsewhere to place a display div hidden _javascript tips

Source: Internet
Author: User
Implementation is also very simple, but it should be noted that in the click of the display of events, you need to do to prevent the event bubbling processing, otherwise trigger the page click event. But there is also a disadvantage, that is, if the same page if there are events to prevent bubbling, you can not hide the div, so in such an event requires special treatment under: their own call to hide under the div (but normally this is not a lot of events);
Js:
Copy Code code as follows:

$ (document). Ready (function () {
Language Head Click event, display language list
$ (". language_selected"). Click (function (e) {
$ (". Language_list"). Toggle ();
E.stoppropagation (); Prevent event bubbling, or the event will bubble to the following document click event
});
Hide Language list when clicking on a document
$ (document). Click (function () {
$ (". Language_list"). Hide ();
});
When you click a language item in the Language list, update the selected item and hide the language list
$ (". Language_list li"). Click (function () {
$ (". language_selected"). Text ($ (this). text ());
$ (". Language_list"). Hide ();
});
$ ("#noPopEvent"). Click (function (e) {
E.stoppropagation ();
});
});

Css:
Copy Code code as follows:

. language_selected
{
Cursor:pointer;
}
. language_list
{
BORDER:1PX solid black;
Display:none;
}
. language_list Li
{
Cursor:pointer;
border:1px solid red;
}

Html:
Copy Code code as follows:

<div style= "width:200px" >
<div class= "language_selected" >
Chinese (Simplified) </div>
<div class= "Language_list" >
<ul>
<li> Chinese (Simplified) </li>
<li>English</li>
</ul>
</div>
</div>
<div id= "nopopevent" style= "width:100px; height:100px; border:1px solid black; " >
Click me, do not hide the language list, need to display div yourself
</div>

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.