Home > Article > Web Front-end > js prevents default events and js prevents event bubbling examples to share js prevents bubbling events
Nested p elements, if both the parent and child elements are bound to some events, then the event of the parent element may be triggered when the innermost child element is clicked. Here is an introduction to js blocking default events and js blocking events. Bubbling example, please refer to it
1. event.preventDefault(); – Prevent the default event of the element.
Note: The default event for click jump of a element,
default event for button, radio and other form elements,
p element has no default event
Example:
<a href="http://www.baidu.com" target="_black">百度</a>var samp = document.getElementByTagName("a"); samp.addEventListener("click",function(e){e.preventDefault()},false);
Explanation: When a link is clicked, a jump will occur under normal circumstances, but now we have blocked its default event, that is, the jump event, so that it will not jump to Baidu.
event.stopPropagation(); – Prevent element bubbling events
Note: Nested elements generally have bubbling events, which will have certain effects
Example:
<p id="c1" onclick="alert(1)"> <p id="c2" onlick="alert(2)"> <input type="button" id="c3" value="点击" onclick="alert(3)"> </p> </p>
When the button is clicked here, the browser will pop up 3, 2, and 1 successively. Originally, I only wanted the event bound to the button to occur, but it was triggered inadvertently. For the events on the two parents, here we just did a simple test. If during project development, a button and its parent were bound to important events at the same time, the results would be disastrous. The solution at this time is to prevent bubbling events.
Register click events for input and prevent its bubbling events
document.getElementById('c3').addEventListener('click',function(e){e.stopPropagation()},false);
For nested p elements, if both the parent and child elements are bound to some events, then when the innermost child element is clicked Events of parent elements may be triggered. Here is an example of js preventing default events and js preventing event bubbling. Please refer to it
1. event.preventDefault(); – Prevent the default event of an element.
Note: The default event for click jump of a element,
default event for button, radio and other form elements,
p element has no default event
Example:
<a href="http://www.baidu.com" target="_black">百度</a>var samp = document.getElementByTagName("a"); samp.addEventListener("click",function(e){e.preventDefault()},false);
Explanation: When a link is clicked, a jump will occur under normal circumstances, but now we have blocked its default event, that is, the jump event, so that it will not jump to Baidu.
event.stopPropagation(); – Prevent element bubbling events
Note: Nested elements generally have bubbling events, which will have certain effects
Example:
<p id="c1" onclick="alert(1)"> <p id="c2" onlick="alert(2)"> <input type="button" id="c3" value="点击" onclick="alert(3)"> </p> </p>
When the button is clicked here, the browser will pop up 3, 2, and 1 successively. Originally, I only wanted the event bound to the button to occur, but it was triggered inadvertently. For the events on the two parents, here we just did a simple test. If during project development, a button and its parent were bound to important events at the same time, the results would be disastrous. The solution at this time is to prevent bubbling events.
Register the click event for the input and prevent its bubbling event
document.getElementById('c3').addEventListener('click',function(e){e.stopPropagation()},false);
This article explains the examples of js blocking default events and js blocking event bubbling. Sharing js blocking bubbling events, please pay attention to more related content. php Chinese website.
Related recommendations:
Common writing methods and calling methods of js functions
The whole process of AJAX reading json of native JS
vue.js installation and configuration
The above is the detailed content of js prevents default events and js prevents event bubbling examples to share js prevents bubbling events. For more information, please follow other related articles on the PHP Chinese website!