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 相关文章推荐
关于文本框的一些限制控制总结~~
Apr 15 Javascript
JS获取计算机mac地址以及IP的实现方法
Jan 08 Javascript
js隐式全局变量造成的bug示例代码
Apr 22 Javascript
javascript中for/in循环及使用技巧
Sep 01 Javascript
jqTransform美化表单
Oct 10 Javascript
JavaScript实现标题栏文字轮播效果代码
Oct 24 Javascript
javascript中Date format(js日期格式化)方法小结
Dec 17 Javascript
bootstrap 表单验证使用方法
Jan 11 Javascript
jQuery实现简单的回到顶部totop功能示例
Oct 16 jQuery
Vue.set()动态的新增与修改数据,触发视图更新的方法
Sep 15 Javascript
JS实现的贪吃蛇游戏完整实例
Jan 18 Javascript
Vue递归组件+Vuex开发树形组件Tree--递归组件的简单实现
Apr 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
如何使用脚本模仿登陆过程
2006/11/22 PHP
php读取EXCEL文件 php excelreader读取excel文件
2012/12/06 PHP
php之Memcache学习笔记
2013/06/17 PHP
php实现encode64编码类实例
2015/03/24 PHP
PHP环境中Memcache的安装和使用
2015/11/05 PHP
PHP预定义变量9大超全局数组用法详解
2016/04/23 PHP
老生常谈PHP面向对象之命令模式(必看篇)
2017/05/24 PHP
PHP实现的mysql主从数据库状态检测功能示例
2017/07/20 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
2018/02/07 PHP
jQuery find和children方法使用
2011/01/31 Javascript
js中的onchange和onpropertychange (onchange无效的解决方法)
2014/03/08 Javascript
js、jquery图片动画、动态切换示例代码
2014/06/03 Javascript
js基于面向对象实现网页TAB选项卡菜单效果代码
2015/09/09 Javascript
bootstrap输入框组使用方法
2017/02/07 Javascript
利用Vue.js框架实现火车票查询系统(附源码)
2017/02/27 Javascript
Nodejs中Express 常用中间件 body-parser 实现解析
2017/05/22 NodeJs
JS实现移动端整屏滑动的实例代码
2017/11/10 Javascript
详解如何写出一个利于扩展的vue路由配置
2019/05/16 Javascript
微信小程序实现左侧滑动导航栏
2020/04/08 Javascript
vue实现给div绑定keyup的enter事件
2020/07/31 Javascript
Python yield使用方法示例
2013/12/04 Python
python文件和目录操作方法大全(含实例)
2014/03/12 Python
jupyter安装小结
2016/03/13 Python
Python 使用SMTP发送邮件的代码小结
2016/09/21 Python
django_orm查询性能优化方法
2018/08/20 Python
浅谈python3.x pool.map()方法的实质
2019/01/16 Python
使用python远程操作linux过程解析
2019/12/04 Python
python代码如何实现余弦相似性计算
2020/02/09 Python
简单了解pytest测试框架setup和tearDown
2020/04/14 Python
HTML5新增的标签和属性归纳总结
2018/05/02 HTML / CSS
美国踏板车和轻便摩托车销售网站:Mega Motor Madness
2020/02/26 全球购物
业务员岗位职责范本
2013/12/15 职场文书
大学生活动策划方案
2014/02/10 职场文书
新闻专业学生的自我评价
2014/02/13 职场文书
英语感谢信范文
2015/01/20 职场文书
导游词之天津盘山
2019/11/01 职场文书