How to correctly use preventDefault(), stopPropagation(), or return false; on events

I’m sure this has been written about many times before and probably has hundreds of answers on StackOverflow.

Despite this we still find ourselves going through code bases and repeatedly finding the misuse (or interchangeable use, or combined use) of event.preventDefault(), event.stopPropagation() and return false;.

So today we’re going to learn what the differences are between the three, and exactly how they function.

preventDefault(), stopPropagation(), and return false; are not interchangeable, nor are they tools of trial-and-error.

We’ll start off with a pretty common UI pattern — a file upload panel — and see how each of them affect its behaviour.

https://github.com/FineUploader/fine-uploader

Image courtesy of FineUploader

https://fineuploader.com/demos.html

HTML

<div class="file-upload">
<input type="file" name="upload-file" class="file-upload__input" style="display: none;" /> <div class="file-upload__drop-zone">
<span class="file-upload__drop-zone-text">Drop files here</span>
<a href="#" class="file-upload__btn--upload">Upload files</a>
</div>
</div>

上面的html,dropzone是button的父控件(之后的事件处理,涉及到事件冒泡)

Our HTML consists of three parts:

  1. An input to handle the file upload dialog. This is hidden (display: none;) , as we will be triggering the upload dialog using the following two elements.
  2. A div with the class of file-upload__dropzone which acts as the main ‘drop zone’ where we will be able to drag-and-drop files (code not included) or click to open a file upload dialog.
  3. An a tag with the class of file-upload__btn--upload which will act as the “Upload files” button, which when clicked will open a file upload dialog.

html的页面效果如下

左边是drop files here,右边是Upload files的link

JavaScript

function fileUpload() {
document.querySelector('.file-upload__input').click();
} const dropzone = document.querySelector('.file-upload__drop-zone');
const button = document.querySelector('.file-upload__btn--upload'); dropzone.addEventListener('click', fileUpload);
button.addEventListener('click', fileUpload);

上面的js代码,在页面加载的时候,不会调用函数,只是实例化函数。

然后拿到dropzone和button,然后给他们绑定上了click事件,并且click事件的handler都是fileUpload函数

Our JavaScript, like our HTML, also consists of three parts:

  1. A fileUpload function to trigger the click event on the file upload input.
  2. Assigning both the dropzone div and a button to variables.
  3. Adding event listeners to those, which when clicked invoke the fileUpload function.

If we were to try this out now, we may see some odd behaviour — after the first dialog has opened and we have chosen our file, a second one will open prompting us again. Keep reading and all will be revealed.

在FileUpload方法中打印event的currentTarget可以看出来,是谁触发了事件

 function fileUpload(event) {
console.log(event.currentTarget);
document.querySelector('.file-upload__input').click();
}

event.preventDefault()

Prevents the browsers default behaviour (such as opening a link), but does not stop the event from bubbling up the DOM.

In our scenario, clicking on the “Upload files” button will invoke the fileUpload function, as we would expect.

Being an a tag, however, it also has a default behaviour — this being to navigate the browser to the location in the href attribute. In this instance we have this set to #, which in most browsers will just cause the page to jump back to the top.

Jumping back to the top of the page is not really our desired behaviour, so we can prevent this by using the preventDefault method. This prevents the default behaviour of an element.

Modifying our JavaScript code, we can fix this so that clicking the link prevents the default behaviour of navigating to the location in the href attribute, but still opens the file upload dialog.

dropzone.addEventListener('click', fileUpload);

button.addEventListener('click', (event) => {
event.preventDefault();
fileUpload();
});

Here we have taken the click event and prevented its default behaviour using event.preventDefault(), then invoked the fileUpload() function.

We still have the dialog box popping up twice, but hopefully the next section will solve this issue.

event.stopPropagation()

Prevents the event from bubbling up the DOM, but does not stop the browsers default behaviour.

For an in-depth explanation of event bubbling, I’d recommend this article about event propagation.  https://www.sitepoint.com/event-bubbling-javascript/

But essentially, when an event is called on an element, that event bubbles up the DOM and gets called on all of the elements parents.

In our case, that means that when we click on the “File upload” button, that click event is also called on all of its parent elements, including our dropzone.

To see this in action, we can remove the fileUpload() call in the button event listener and the function will still be invoked when we click on the button because the click event will bubble up the DOM and be called on the dropzone.

dropzone.addEventListener('click', fileUpload);

button.addEventListener('click', event => event.preventDefault());

This bubbling is an example of event propagation, which is where the stopPropagation method comes into play.

We can use it to prevent this default bubbling behaviour so that the event is only registered by the element it is called upon.

event propagation包括bubble和capture  What is event bubbling and capturing?

dropzone.addEventListener('click', fileUpload);

button.addEventListener('click', event => event.stopPropagation());

按照上面进行修改后,点击link,不会触发dropzone的click。只有点击dropzone,才会触发click

Now we see that not only does the click event not bubble up the DOM, but by removing the preventDefault method call the a tag acts as it should again, by navigating to its href attribute.

点击link后,会触发link本身的default behavior,点击link后,页面会跳转到一个新的url http://172.31.212.20:8082/FineUploaderTest/#,这个新的url后面带#

但是我们期待做以下三件事情

But this isn’t what we want. We wanted to call the fileUpload function and also prevent the element’s default behaviour and prevent the event from bubbling up the DOM.

We could use both preventDefault and stopPropagation then call the fileUpload function, like so.

dropzone.addEventListener('click', fileUpload);

button.addEventListener('click', (event) => {
event.preventDefault();
event.stopPropagation();
fileUpload();
});

return false;

Usually seen in jQuery code, it Prevents the browsers default behaviour, Prevents the event from bubbling up the DOM, and immediately Returns from any callback.

In vanilla JavaScript, returning false doesn’t have any effect on the default behaviour or event propagation of the element, as we can see here, it acts exactly as it did at the start.

dropzone.addEventListener('click', fileUpload);

button.addEventListener('click', (event) => {
fileUpload();
return false;
});

It calls the click event on the button, also navigating to it’s href value, then bubbles up the DOM, calling the click event on the dropzone too.

原生的js里面,return false没有意义。还是会navigate到href,然后事件冒泡到父控件,触发 父控件的click

However…

…in the context of jQuery, returning false will immediately exit the event listeners callback. This has the effect of both:

  1. Preventing the default behaviour — navigating the browser to the a tag’s href attribute.
  2. Stopping any event propagation — stopping the click event from bubbling up the DOM.

If we refactor our code to jQuery, we can see this in practice.

const dropzone = $('.file-upload__drop-zone');
const button = $('.file-upload__btn--upload'); $(dropzone).on('click', fileUpload); $(button).on('click', (event) => {
fileUpload();
return false;
});

We call the fileUpload method, then return false to prevent any default behaviour or event propagation.

Conclusion

We should use these tools correctly and wisely.

Next time when we’re in this kind of situation, we shouldn’t just play around with event.preventDefault(), event.stopPropagation() and return false; until we get the desired result.

We should think what it is we want to achieve, and how to get there — not through trial-and-error and luck — but through thinking through the problem and applying the correct solution.

How to correctly use preventDefault(), stopPropagation(), or return false; on events的更多相关文章

  1. e.preventDefault()和e.stopPropagation()以及return false的作用和区别

    前段时间开发中,遇到一个父元素和子元素都有事件时,发现会出现事件冒泡现象,虽然知道ev.stopPropagation()和ev.preventDefault()其中一个是阻止事件冒泡和阻止默认行为, ...

  2. JS preventDefault ,stopPropagation ,return false

    所谓的事件有两种:监听事件和浏览器对特殊标签元素的默认行为事件.监听事件:在节点上被监听的事件操作,如 select节点的change事件,a节点的click事件.浏览器的默认事件:特定页面元素上带的 ...

  3. js 阻止事件冒泡和默认行为 preventDefault、stopPropagation、return false

    preventDefault: preventDefault它是事件对象(Event)的一个方法,作用是取消一个目标元素的默认行为.既然是说默认行为,当然是元素必须有默认行为才能被取消,如果元素本身就 ...

  4. preventDefault()、stopPropagation()、return false 之间的区别

    “return false”之所以被误用的如此厉害,是因为它看起来像是完成了我们交给它的工作,浏览器不会再将我们重定向到href中的链接,表单也不会被继续提交,但这么做到底有什么不对呢? 可能在你刚开 ...

  5. preventDefault()、stopPropagation()、return false 的区别

    preventDefault() e.preventDefault()阻止浏览器默认事件 stopPropagation() e.stopPropagation()阻止冒泡 return false ...

  6. 阻止事件冒泡两种方式:event.stopPropagation();和return false;

    jQuery提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function (event) { ...

  7. e.preventDefault() e.stopPropagation()和return false的区别

    e.preventDefault(); //阻止事件的默认行为,比如a标签的转向,但不阻止事件的冒泡传播e.stopPropagation() //阻止事件的冒泡传播,但不阻止其默认行为returne ...

  8. preventDefault, stopPropagation, return false -JS事件处理中的坑

    我们以一个文件上传ui重设计为例子来探讨这几个函数的区别: 其中的html代码如下: <div class="file-upload"> <input type= ...

  9. 【转】stopPropagation, preventDefault 和 return false 的区别

    因为有父, 子节点同在, 因为有监听事件和浏览器默认动作之分. 使用 JavaScript 时为了达到预期效果经常需要阻止事件和动作执行. 一般我们会用到三种方法, 分别是  stopPropagat ...

随机推荐

  1. 关于redis的几件小事(六)redis的持久化

    1.redis持久化的意义 redis持久化的意义,在于 故障恢复 . 如果没有对数据进行持久化,那么如果redis遇到灾难性的故障,就会丢失所有的数据. 如果通过redis的持久化机制将数据持久化到 ...

  2. Pornhub Web 开发者访谈

    原文:Interview with a Pornhub Web Developer 译者:neal1991 welcome to star my articles-translator, provid ...

  3. 关于ES5中的prototype与ES6中class继承的比较

    ES5:继承: 1.ES5:继承 通过原型链实现继承.子类的prototype为父类对象的一个实例,因此子类的原型对象包含指向父类的原型对象的指针,父类的实例属性成为子类原型属性 2.ES6 的继承 ...

  4. python如何判断字符串是否以某个字母或者数字结尾

    1.如果是对某个确定的字符或者数字进行判断,可以直接使用endswith()方法 # 判断str_a是否以‘A’结尾 str_a = '20190813A' print(str_a.endswith( ...

  5. 【项目构建工具】 Gradle笔记2

    一.Gradle执行流程 1.Gradle的执行流程(生命周期)主要是三个阶段: 初始化阶段:解析整个工程中所有Project,构建所有的Project对应的project对象 配置阶段:解析所有的p ...

  6. 自动化测试报告之allure使用基础指南

    差不多三个月前些的教程,然后跳槽了,自定义模块还没有写....后续也不知道有时间补上没有,最近应该会毕竟专注app测试这块了     1.github下载allure安装包:https://githu ...

  7. 黑客正在使用美国NSA泄露的工具进行挖矿

    早些年我们知道美国国家安全局囤积不少漏洞准备自己使用,结果这些漏洞以及利用工具被方程式组织获得. 随后名为影子经纪人的黑客组织获得这些漏洞和工具后又再次出售,当初的永恒之蓝漏洞就是从这里泄露的. 永恒 ...

  8. xml配置文件命名空间学习

    xmlns(XML Namespaces的缩写)是一个属性,是XML(标准通用标记语言的子集)命名空间.作用是赋予命名空间一个唯一的名称. 编写Spring或者Maven或者其他需要用到XML文档的程 ...

  9. 标准C语言(5)

    无法预知的数字叫随机数,rand标准函数可以用来获得随机数,为了使用这个标准函数需要包含stdlib.h头文件 srand标准函数用来设置随机数种子,这个函数把一个整数作为种子使用不同的种子可以得到不 ...

  10. backtop返回页面顶部jquery代码

    <div id="toTop" style="width:30px;height:110px;border:1px solid #74B9DC; border-ra ...