Analysis on the correct use of return false

Source: Internet
Author: User

When you first learn about jQuery event processing, the first example is how to prevent the browser from executing the default action. For example, the following code demonstrates the click event:
Copy codeThe Code is as follows:
$ ("A. toggle"). click (function (){
$ ("# Mydiv"). toggle ();
Return false; // Prevent browser from visiting '#'
});

This function uses toggle to show or hide # mydiv, and then prevents the browser from continuing to access the link specified in href.

The example above will give users a bad habit of using "return false" to prevent browsers from executing default behaviors. In this article, I will discuss two important topics that prevent the browser from executing default behaviors:

• Select the correct method:Return false or preventDefault, stopPropagation or stopImmediatePropagation
• Select a proper location, start, end, or a place in the middle:Which part of the Event Callback should you cancel the default browser action?

Note:When I mention event bubbling in this article, I want to express that most events are triggered on the initial DOM first, and then go up through the DOM tree, triggered on each parent element, the event is not bubbling on the sibling node or child node (when the event is bubbling down, we call it event capturing )), you can learn more here

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.