jquery implements the mouse click outside the div area to hide the div

Source: Internet
Author: User

Bubble definition: When an event on an element is triggered, such as a mouse click on a button, the same event will be triggered in all ancestor elements of that element. This process is known as event bubbling, which bubbles to the top of the DOM tree from the beginning of the original element. (Excerpt from the network)

1. Block event bubbling and do not block event behavior: event.stoppropagation ();
$(function(){$("#link").  Click(function(event){event.  Stoppropagation();}); }); 
2. Block event behavior and block event bubbling: Event.preventdefault ();
$(function(){$("#link").  Click(function(event){event.  Preventdefault();  }); });

3. Block event bubbling and also block event behavior: return false;
$ ( Function ()  {$ ( "#link" ). click (function (event   {returnfalse; })            /span>                



Block event bubbling:
$ ("#btn"). Click (Function (Event) {$ ("#demo") ). FadeIn (); $ (document). One ("click", Function () {//) bind a shadow div method $ ("#demo") to document. Hide ();}); Event.stoppropagation ();//click button to block the event from bubbling to document}); $ ("#demo"). Click (Function (event) {event.stoppropagation ();//clicking event in div area prevents bubbling to document}); 

Example:

 <p><a href= "javascript:void (0)" class= "A" > Menu button </a></p><div class= "Menu" ><a href= "http://www.baidu.com" > Baidu </a></div>$ ( ". a"). On ("click", function   (e) { if  ($ (". Menu"). Is (": Hidden" . Menu " else  {$ ( ". Menu" ). Hide (); } $ (document). One ( "click", function   () {$    (. Menu " ". Menu"). On ("click", function   (e) { E.stoppropagation ();});  

JQuery implements a mouse click outside the div area to hide the 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.