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 ()