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

 更新时间:2010年07月11日 12:29:38   作者:  
Have you ever seen those two things (in the title) being used in jQuery? Here is a simple
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:

演示地址: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

测试代码打包下载

您可能感兴趣的文章:

相关文章

  • JS创建对象的六种方式

    JS创建对象的六种方式

    在js中,对象是键值对的集合,可以通过很多种方式创建; 本文给大家介绍了六种创建方式:对象字面量、构造函数、Object.create()、工厂函数、class、单例模式,需要的朋友可以参考下
    2025-04-04
  • js的隐含参数(arguments,callee,caller)使用方法

    js的隐含参数(arguments,callee,caller)使用方法

    本篇文章只要是对js的隐含参数(arguments,callee,caller)使用方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • JavaScript仿聊天室聊天记录

    JavaScript仿聊天室聊天记录

    这篇文章主要为大家详细介绍了JavaScript仿聊天室聊天记录实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • JavaScript实现将UPC转换成ISBN的方法

    JavaScript实现将UPC转换成ISBN的方法

    这篇文章主要介绍了JavaScript实现将UPC转换成ISBN的方法,涉及javascript字符串操作的相关技巧,需要的朋友可以参考下
    2015-05-05
  • JS之相等操作符详解

    JS之相等操作符详解

    下面小编就为大家带来一篇JS之相等操作符详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • 基于JS实现数字动态变化显示效果附源码

    基于JS实现数字动态变化显示效果附源码

    我们经常看到液晶电子表样式,数字动态显示,动态变化的在指定元素内显示数字。怎么实现效果呢?下面小编给大家带来了基于JS实现数字动态变化显示效果 ,感兴趣的朋友一起看看吧
    2019-07-07
  • 菜单制作学习一个小东西

    菜单制作学习一个小东西

    菜单制作学习一个小东西...
    2006-09-09
  • js阻止事件追加的具体实现

    js阻止事件追加的具体实现

    可以使用 e.stopPropagation(); e.preventDefault();来阻止事件冒泡,和默认事件的执行。但不能阻止事件的追加,如要追加,请看下面的实现方法
    2014-10-10
  • 利用location.hash实现跨域iframe自适应

    利用location.hash实现跨域iframe自适应

    其他一些类似js跨域操作问题也可以按这个思路去解决,需要的朋友可以测试下。
    2010-05-05
  • 使用JavaScript实现轮播图效果完整实例

    使用JavaScript实现轮播图效果完整实例

    我们在各种网页中经常见到轮播图的效果,下面这篇文章主要给大家介绍了关于使用JavaScript实现轮播图效果的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01

最新评论