return false;和e.preventDefault();的区别


Posted in Javascript onJuly 11, 2010

Have you ever seen those two things (in the title) being used in jQuery? Here is a simple example:

$("a").click(function() { 
$("body").append($(this).attr("href")); 
return false; 
}

That code would append the href attribute as text to the body every time a link was clicked but not actually go to that link. The return false; part of that code prevents the browser from performing the default action for that link. That exact thing could be written like this:
$("a").click(function(e) { 
$("body").append($(this).attr("href")); 
e.preventDefault(); 
}

So what's the difference?

The difference is that return false; takes things a bit further in that it also prevents that event from propagating (or “bubbling up”) the DOM. The you-may-not-know-this bit is that whenever an event happens on an element, that event is triggered on every single parent element as well. So let's say you have a box inside a box. Both boxes have click events on them. Click on the inner box, a click will trigger on the outer box too, unless you prevent propagation. Like this:
return false;和e.preventDefault();的区别
演示地址:http://css-tricks.com/examples/ReturnFalse/
So in other words:

function() { 
return false; 
} // IS EQUAL TO 
function(e) { 
e.preventDefault(); 
e.stopPropagation(); 
}

It's all probably a lot more complicated than this and articles like this probably explain it all a lot better.

参考:

1.The difference between ‘return false;' and ‘e.preventDefault();'
2.Event order

测试代码打包下载

Javascript 相关文章推荐
js 数组实现一个类似ruby的迭代器
Oct 27 Javascript
jQuery javaScript捕获回车事件(示例代码)
Nov 07 Javascript
深入理解Javascript中this的作用域
Aug 12 Javascript
JavaScript数据类型检测代码分享
Jan 26 Javascript
jquery对象和DOM对象的相互转换详解
Oct 18 Javascript
解决拦截器对ajax请求的拦截实例详解
Dec 21 Javascript
深入理解vue中slot与slot-scope的具体使用
Jan 26 Javascript
VUE实现可随意拖动的弹窗组件
Sep 25 Javascript
jQuery选择器之基本过滤选择器用法实例分析
Feb 19 jQuery
JS判断数组里是否有重复元素的方法小结
May 21 Javascript
JavaScript中0、空字符串、'0'是true还是false的知识点分享
Sep 16 Javascript
基于Vue sessionStorage实现保留搜索框搜索内容
Jun 01 Javascript
基于jQuery的树控件实现代码(asp.net+json)
Jul 11 #Javascript
js 模拟气泡屏保效果代码
Jul 10 #Javascript
浅谈javascript的数据类型检测
Jul 10 #Javascript
jquery nth-child()选择器的简单应用
Jul 10 #Javascript
SWFObject 2.1以上版本语法介绍
Jul 10 #Javascript
加载jQuery后$冲突的解决办法
Jul 09 #Javascript
在javascript将NodeList作为Array数组处理的方法
Jul 09 #Javascript
You might like
PHP insert语法详解
2008/06/07 PHP
PHP简洁函数小结
2011/08/12 PHP
锋利的jQuery jQuery中的DOM操作
2010/03/21 Javascript
仅IE不支持setTimeout/setInterval函数的第三个以上参数
2011/05/25 Javascript
网站内容禁止复制和粘贴、另存为的js代码
2014/02/26 Javascript
JavaScript模块随意拖动示例代码
2014/05/27 Javascript
JavaScript学习笔记之JS事件对象
2015/01/22 Javascript
javascript中局部变量和全局变量的区别详解
2015/02/27 Javascript
jQuery实现表格行上移下移和置顶的方法
2015/05/22 Javascript
移除AngularJS下URL中的#字符的方法
2015/06/19 Javascript
AngularJS控制器详解及示例代码
2016/08/16 Javascript
手机端图片缩放旋转全屏查看PhotoSwipe.js插件实现
2016/08/25 Javascript
详解vue-Resource(与后端数据交互)
2017/01/16 Javascript
解决Vue页面固定滚动位置的处理办法
2017/07/13 Javascript
实现单层json按照key字母顺序排序的示例
2017/12/06 Javascript
vue 动态绑定背景图片的方法
2018/08/10 Javascript
JavaScript中toLocaleString()和toString()的区别实例分析
2018/08/14 Javascript
jQuery模仿ToDoList实现简单的待办事项列表
2019/12/30 jQuery
SublimeText 2编译python出错的解决方法(The system cannot find the file specified)
2013/11/27 Python
python目录操作之python遍历文件夹后将结果存储为xml
2014/01/27 Python
Python手机号码归属地查询代码
2016/05/04 Python
Python基于动态规划算法解决01背包问题实例
2017/12/06 Python
python实现读Excel写入.txt的方法
2018/04/29 Python
python重试装饰器的简单实现方法
2019/01/31 Python
CSS3悬停效果案例应用
2012/11/21 HTML / CSS
利用html5 file api读取本地文件示例(如图片、PDF等)
2018/03/07 HTML / CSS
HTML5中drawImage用法分析
2014/12/01 HTML / CSS
基于HTML5实现类似微信手机摇一摇功能(计算摇动次数)
2017/07/24 HTML / CSS
为世界各地的女性设计和生产时尚服装:ROMWE
2016/09/17 全球购物
法务专员岗位职责
2014/01/02 职场文书
一岗双责责任书
2014/04/15 职场文书
交通事故协议书范文
2014/10/23 职场文书
事业单位岗位说明书
2015/10/08 职场文书
go语言求任意类型切片的长度操作
2021/04/26 Golang
详解 TypeScript 枚举类型
2021/11/02 Javascript
Win11软件图标固定到任务栏
2022/04/19 数码科技