How to Prevent event bubbling in JQuery and Its Differences

Source: Internet
Author: User

JQuery provides two methods to prevent event bubbles. The following describes the two methods and specific application cases.

Method 1: event. stopPropagation ();

$("#div1").mousedown(function(event){event.stopPropagation();});

Method 2: return false;

$("#div1").mousedown(function(event){return false;});

The two methods differ as follows:

Return false not only prevents the event from bubbling up, but also blocks the event itself. Event. stopPropagation () only prevents the event from bubbling up, not the event itself.

Specific Application Scenario: Google's Lenovo drop-down box. When a drop-down list is displayed, you need to keep the cursor in the text input box when you press the mouse in the drop-down list area.

Sample Test code: when the text input box obtains the focus, event. stopPropagation () is used in the mousedown event of div1. in code, When you click the red area, the cursor in the text input box is lost. When return false is used, the cursor remains in the text input box when you click the Red Area of the Code.

<Html> Articles you may be interested in

 
  • Js shields mouse and keyboard events (including right-click, direction key, backspace key, F5 refresh key, etc.), compatible with IE and firefox
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Js address bar special effects (display the size of all images with links on the page and view the height of the current browser)
  • JQuery adds Event Response to dynamically generated content (jquery live Method Introduction)
  • Differences between variables and functions in php after the static keyword is added
  • JQuery-handle the latency of a hover event
  • Difference between execute and query methods in ThinkPHP
  • Differences in the usage of tinyint, smallint, int, and bigint types in mysql

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.