JQuery JS in return False,e.preventdefault (), e.stoppropagation () difference (event bubbling)

Source: Internet
Author: User

Sometimes it's annoying to encounter bubbling events, really ...

1.e.stoppropagation () block event bubbling
<Head>    <title></title>    <Scriptsrc= "Scripts/jquery-1.4.1.js"type= "Text/javascript"></Script></Head><Body>    <Table>        <TR>            <TD><span>Bubbling event Test</span></TD>        </TR>    </Table></Body>

Let's look at this code first:

   <script type= "Text/javascript" >        $ (function  () {            $ ("table"). Click (  function () {Alert ("table alert");});            $ ("TD"). Click (function () {alert ("TD alert");});            $ ("span"). Click (function  () {                    alert ("span alert");            }     ); </script>

We will see a situation like this: Span alert, TD Alert, table alert. This is called event bubbling. is from bottom to top, from inside to outside, the events are triggered sequentially.

Sometimes we don't want events to bubble up.

<script type= "Text/javascript" >        $ (function  () {            $ ("table"). Click (  function () {Alert ("table alert");});            $ ("TD"). Click (function () {alert ("TD alert");});            $ ("span"). Click (function  (e) {                    alert ("span alert");                          E.stoppropagation ();    } ); </script>

If you want to get event-related information, you should add an E object to the anonymous method, and E is the event object.

2.e.preventdefault () blocks event default behavior.
$ ("a"). Click (function  (e) {     alert ("default behavior is forbidden");     E.preventdefault ();}); <a href= "http://www.baidu.com" > Testing </a>
3.return false

Return false is equivalent to calling both E.preventdefault () and E.stoppropagation ().

Return false prevents event bubbling in addition to blocking the default behavior. If you have a jquery source code on hand, you can see the following code:

if  (ret===false) {event.preventdefault (); Event.stoppropagation ();}

JQuery JS in return False,e.preventdefault (), e.stoppropagation () difference (event bubbling)

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.