What's the difference between event.stopPropagation and event.preventDefault?

后端 未结 7 1276
余生分开走
余生分开走 2020-11-22 00:52

They seem to be doing the same thing...
Is one modern and one old? Or are they supported by different browsers?

When I handle events myself (without framework) I

7条回答
  •  隐瞒了意图╮
    2020-11-22 01:44

    return false;


    return false; does 3 separate things when you call it:

    1. event.preventDefault() – It stops the browsers default behaviour.
    2. event.stopPropagation() – It prevents the event from propagating (or “bubbling up”) the DOM.
    3. Stops callback execution and returns immediately when called.

    Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up.

    preventDefault();


    preventDefault(); does one thing: It stops the browsers default behaviour.

    When to use them?


    We know what they do but when to use them? Simply it depends on what you want to accomplish. Use preventDefault(); if you want to “just” prevent the default browser behaviour. Use return false; when you want to prevent the default browser behaviour and prevent the event from propagating the DOM. In most situations where you would use return false; what you really want is preventDefault().

    Examples:


    Let’s try to understand with examples:

    We will see pure JAVASCRIPT example

    Example 1:

    
    

    Run the above code you will see the hyperlink ‘Click here to visit stackoverflow.com‘ now if you click on that link first you will get the javascript alert Link Clicked Next you will get the javascript alert div Clicked and immediately you will be redirected to stackoverflow.com.

    Example 2:

    
    

    Run the above code you will see the hyperlink ‘Click here to visit stackoverflow.com‘ now if you click on that link first you will get the javascript alert Link Clicked Next you will get the javascript alert div Clicked Next you will see the hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text ‘Click event prevented‘ and you will not be redirected to stackoverflow.com. This is due > to event.preventDefault() method we used to prevent the default click action to be triggered.

    Example 3:

    
    

    This time if you click on Link the function executeParent() will not be called and you will not get the javascript alert div Clicked this time. This is due to us having prevented the propagation to the parent div using event.stopPropagation() method. Next you will see the hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text ‘Click event is going to be executed‘ and immediately you will be redirected to stackoverflow.com. This is because we haven’t prevented the default click action from triggering this time using event.preventDefault() method.

    Example 4:

    
    

    If you click on the Link, the function executeParent() will not be called and you will not get the javascript alert. This is due to us having prevented the propagation to the parent div using event.stopPropagation() method. Next you will see the hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text ‘Click event prevented‘ and you will not be redirected to stackoverflow.com. This is because we have prevented the default click action from triggering this time using event.preventDefault() method.

    Example 5:

    For return false I have three examples and all appear to be doing the exact same thing (just returning false), but in reality the results are quite different. Here's what actually happens in each of the above.

    cases:

    1. Returning false from an inline event handler prevents the browser from navigating to the link address, but it doesn't stop the event from propagating through the DOM.
    2. Returning false from a jQuery event handler prevents the browser from navigating to the link address and it stops the event from propagating through the DOM.
    3. Returning false from a regular DOM event handler does absolutely nothing.

    Will see all three example.

    1. Inline return false.

    
    

    1. Returning false from a jQuery event handler.

    
    
    

    1. Returning false from a regular DOM event handler.

    
    

    Hope these examples are clear. Try executing all these examples in a html file to see how they work.

提交回复
热议问题