Home > Web Front-end > JS Tutorial > body text

Block events (cancel the browser's default behavior for events and prevent them from propagating)_javascript tips

WBOY
Release: 2016-05-16 17:17:56
Original
991 people have browsed it

Cancel the browser's default behavior (response) to the event (such as tag jump, etc.) and stop the event from continuing to propagate.

Implementation code

Copy 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 prevents the event from continuing to propagate (does not cancel the default behavior)
Copy code 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 behavior (do not prevent the event from continuing to propagate)
Copy the code The code is as follows:

function stopEvent (evt) {
var evt = evt || window.event;
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template