Home  >  Article  >  Web Front-end  >  Block events (cancel the browser's default behavior for events and prevent them from propagating)_javascript tips

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

WBOY
WBOYOriginal
2016-05-16 17:17:56987browse

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;
}
}
Statement:
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