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