Block events (cancel the browser's default behavior on events and prevent them from spreading) _ javascript tips-js tutorial

Source: Internet
Author: User
Cancel the default action (response) of the browser on the event (for example, the jump of tag a) and stop the event from spreading, the following is a good example. You can cancel the browser's default event behavior (response) (such as tag jump) and stop the event propagation.

Implementation Code

The Code is as follows:


Function stopEvent (evt ){
Var evt = evt | window. event;
If (evt. preventDefault ){
Evt. preventDefault ();
Evt. stopPropagation ();
} Else {
Evt. returnValue = false;
Evt. cancelBubble = true;
}
}


Only prevent the event from spreading (do not cancel the default action)

The Code is as follows:


Function stopEvent (evt ){
Var evt = evt | window. event;
If (evt. stopPropagation ){
Evt. stopPropagation ();
} Else {
Evt. cancelBubble = true;
}
}


Only cancel the default action (do not stop the event from spreading)

The Code is as follows:


Function stopEvent (evt ){
Var evt = evt | window. event;
If (evt. preventDefault ){
Evt. preventDefault ();
} Else {
Evt. returnValue = false;
}
}

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.