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:
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.
This is parent1 div.
This is child1.
This is parent1 div.
This is parent2 div.
This is child2. Will bubble.
This is parent2 div.