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:
  1. event.preventDefault();
  2. event.stopPropagation();
  3. Stops callback execution and returns immediately when called.
See jquery-events-stop-misusing-return-false for more information.

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, the preventDefault 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. Once preventDefault 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 Parameters
No Return Value
No Exceptions
Ref: http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE