Nested div elements. If the parent and child elements are bound to some events, the parent element event may be triggered when you click the child element at the inmost layer, the following is an example of js blocking default events and js blocking event bubbling. For more information, see section 1. event. preventDefault (); -- blocks default events of elements.
Note: The default event for clicking to jump to element,
Default events for button, radio, and other form elements,
No Default event for the p element
Example:
The Code is as follows:
Baidu
The Code is as follows:
Var samp = document. getElementByTagName ("");
Samp. addEventListener ("click", function (e) {e. preventDefault ()}, false );
Explanation: When you click a link, a jump will normally occur, but now we have blocked its default event, that is, the jump event, so it will not jump to Baidu.
2. event. stopPropagation (); -- block the event of element bubbling
Note: Nested elements generally have bubble events, which may have some impact.
Example:
The Code is as follows:
When you click the button, the browser will pop up 3, 2, 1. Originally, only the events bound to the button were triggered, but the events at the two parent level were accidentally triggered, here we just did a simple test. Imagine if a button is bound to a very important event at the same time with its parent in project development, the results would be miserable. At this time, the solution is to stop the bubble event.
Register a click event for input and block its bubble events.
The Code is as follows:
Document. getElementById ('c3'). addEventListener ('click', function (e) {e. stopPropagation ()}, false );
OK !!! Now