Event.preventdefault ()---event.stoppropagation ()

Source: Internet
Author: User

1, Event.preventdefault () block default behavior

<href= "http://www.baidu.com"> link </a> 
//block default behavior E.preventdefault ();$ ("a"). Click (function() {event.preventdefault (); })            //prevent Browser from default behavior            functionStopdefault (e) {//block default browser action (web)                if(e &&E.preventdefault) E.preventdefault (); //how to block the default action of a function in IE                ElseWindow.event.returnValue=false; return false; }

2, Event.stoppropagation () Stop bubbling

<Divclass= "Demo"onclick= "alert (' div ')">        <ulonclick= "alert (' ul ')">            <Lionclick= "alert (' Li ')">Asd</Li>        </ul>    </Div>
//prevent bubbling e.stoppropagation (e);$ (". Demo li"). Click (function(e) {window.event? Window.event.cancelBubble =true: E.stoppropagation (); })            functionstopbubble (e) {//If an event object is provided, this is a non-IE browser                if(e &&e.stoppropagation)//so it supports the Stoppropagation () methode.stoppropagation (); Else                     //Otherwise, we need to use IE to cancel the event bubblingWindow.event.cancelBubble =true; }            }

Event.preventdefault ()---event.stoppropagation ()

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.