Home > Web Front-end > JS Tutorial > Detailed explanation of preventDefault and stopPropagation in js_javascript skills

Detailed explanation of preventDefault and stopPropagation in js_javascript skills

WBOY
Release: 2016-05-16 17:01:38
Original
1231 people have browsed it

First, let’s explain the difference between preventDefault and stopPropagation methods in js:
What does the preventDefault method do? We know that for example Baidu, this is the most basic thing in HTML, and its function is to click the Baidu link to http://www .baidu.com, this is the default behavior of the tag, and the preventDefault method can prevent its default behavior from occurring and other things occur. Everyone will understand after looking at a piece of code:

Copy the code The code is as follows:





JS prevents link jump




Clicking the Baidu link at this time will not open http://www.baidu.com, but will only pop up an alert dialog box.

PreventDefault method has been explained here, what about stopPropagation method? Before talking about the stopPropagation method, I must first explain the js event proxy.

The event proxy uses two features that are often overlooked in JavaSciprt events: event bubbling and target elements. When an event is triggered on an element, such as a mouse click on a button, the same event will be triggered on all ancestor elements of that element. This process is called event bubbling; the event bubbles up from the original element to the top of the DOM tree. For any event, the target element is the original element, which in our case is the button. Target Element It appears as an attribute in our event object. Using an event proxy, we can add an event handler to an element, wait for the event to bubble up from its child elements, and easily determine which element the event started from.

The stopPropagation method is to prevent js events from bubbling up. Take a look at a piece of code.

Copy code The code is as follows:




Prevent JS events from popping up Bubble delivery (cancelBubble, stopPropagation)

<script><br>function doSomething (obj,evt) {<br> alert(obj.id);<br> var e=(evt)?evt:window.event;<br> if (window.event) {<br> e.cancelBubble= true;// Prevent bubbling in ie<br> } else {<br> //e.preventDefault();<br> e.stopPropagation();// Prevent bubbling in other browsers<br> }<br>}<br></script>



This is parent1 div.



This is child1.



This is parent1 div.






This is parent2 div.



This is child2. Will bubble.



This is parent2 div.






You will understand after running the above code .
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