Event handling in jquery: return FALSE, block default behavior, block bubbling, and compatibility issues

Source: Internet
Author: User

Transferred from: http://wyqbailey.diandian.com/post/2012-07-12/40030551971

return False

In jquery, we often use return false to block the browser's default behavior, what does "return false" actually do?

When you call "return false" every time, it actually does 3 things:

    • Event.preventdefault ();

    • Event.stoppropagation ();

    • Stops the callback function execution and returns immediately.

Yes, you are right, return false does so many things. It's been used repeatedly because it looks like it's done with our blocking tasks, and the statements are simple.

There are only preventdefault in these 3 things that prevent the browser from continuing to perform the default behavior, unless you want to stop the event bubbling, or use return False to bury your code in great danger.

Preventdefault ()

In most cases, when you use return false, what you actually need is e.preventdefault (). To use E.preventdefault, you need to make sure that you pass the event parameter to your rollback function. It will do all the work for us, but it will not prevent the parent node from continuing to handle the event, but keep in mind that the less restrictions you put in your code, the more flexible your code is and the easier it is to maintain.

But in use you will find that Preventdefault has compatibility issues, the old version of IE does not care about this method, still go their own way, at this time we need a bit of compatible code to fix ie:

Window.event.returnvalue= false;//return value set to False
Window.event.keyCode = 0;//If you want to prevent the default behavior of the keyboard, such as F5, this is also required

Stoppropagation ()

In some cases, you may need to stop the event bubbling and use stoppropagation directly.

Stopimmediatepropagation ()

This method stops the continuation of related events on the object, even if other handler functions are bound on the current object. Sometimes your code is very complex, and different widgets and plugin can add events on the same object, and if you do, you need to understand and use stopimmediatepropagation.

Event handling in jquery: return FALSE, block default behavior, block bubbling, and compatibility issues

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.