Cancel the default even behaviour of the browser: preventDefault, return false;
First off, return false is actually doing three very separate things when you call it:
- event.preventDefault();
- event.stopPropagation();
- Stops callback execution and returns immediately when called.
For example:
1. In the case of clicking on links, return false will prevent navigation, the href attribute specifies the link's destination will be ignored.
<a href='#' onclick='Func(); return false;'>Click</a>
<a href="http://www.w3schools.com/" onclick="alert( );event.preventDefault();">w3schools</a>
<a href="http://www.w3schools.com/" onclick="return (confirm('Follow this link?'))">w3schools</a>
2. In form submit handlers
<button type="submit" onclick="return confirm('Do you want to submit?');">Submit</button>
preventDefault
- If an event is cancelable, the
preventDefault
method is used to signify that the event is to be canceled, meaning any default action normally taken by the implementation as a result of the event will not occur. If, during any stage of event flow, thepreventDefault
method is called the event is canceled. Any default action associated with the event will not occur. Calling this method for a non-cancelable event has no effect. OncepreventDefault
has been called it will remain in effect throughout the remainder of the event's propagation. This method may be used during any stage of event flow.
No ParametersNo Return ValueNo Exceptions
评论
发表评论