Home > Web Front-end > JS Tutorial > js prevents default events and js prevents event bubbling examples to share js prevents bubbling events

js prevents default events and js prevents event bubbling examples to share js prevents bubbling events

jacklove
Release: 2018-06-15 16:48:22
Original
1681 people have browsed it

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);
Copy after login
Copy after login

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.

  1. 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>
Copy after login
Copy after login

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(&#39;c3&#39;).addEventListener(&#39;click&#39;,function(e){e.stopPropagation()},false);
Copy after login

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);
Copy after login
Copy after login

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.

  1. 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>
Copy after login
Copy after login

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(&#39;c3&#39;).addEventListener(&#39;click&#39;,function(e){e.stopPropagation()},false);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template