Home >Web Front-end >JS Tutorial >Introduction to several ways to prevent event bubbling in JQuery and their differences_jquery

Introduction to several ways to prevent event bubbling in JQuery and their differences_jquery

WBOY
WBOYOriginal
2016-05-16 17:03:391365browse

JQuery provides two ways to prevent events from bubbling.

Method 1: event.stopPropagation();

Copy code The code is as follows:

$("#div1").mousedown(function(event){
event.stopPropagation();
});

Method 2: return false;
Copy code The code is as follows:

$("#div1").mousedown(function(event) {
return false;
});

But there is a difference between these two methods. Returning false not only prevents the event from bubbling up, but also prevents the event itself.

event.stopPropagation() only prevents the event from bubbling up, but does not prevent the event itself.
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