原生js中stopPropagation,preventDefault,return false的区别
1、stopPropagation:阻止事件的冒泡,但不阻止事件的默认行为。
最好莫过于用例子说明:
<div id='div' onclick='alert("div");'>
<ul onclick='alert("ul");'>
<li id='ul-a' onclick='alert("li");'><a href="http://www.baidu.com" id="link">baidu.com</a></li>
</ul>
</div>
a.addEventListener("click",function(event){
event.stopPropagation();
},false);
在FF上执行,不会弹出li,ul,div的弹出框,而是直接跳转。在上面定义的事件中,侦听的是事件冒泡阶段(第三个参数为false,若为true则是捕获阶段)。IE不支持事件流,在IE中定义事件处理程序需要使用attachEvent:
a.attachEvent("onclick",function(event){event.cancelBubble = true;});
2、preventDefault:阻止事件的默认行为,但不阻止冒泡。
在FF,chrome,Safari,opera有效:
a.addEventListener("click",function(event){
event.preventDefault();
},false);
在IE有效:
a.attachEvent("onclick",function(event){event.returnValue = false;});
浏览器执行,会弹出li,ul,div的弹出框,但不会跳转。
3、return false:看到网上说return false会阻止事件的冒泡,但是应该要看定义事件使用的是什么方式
若使用a.onclick或a.attachEvent,则 return false会阻止默认行为;
若使用a.addEventListener,则return false 不会阻止默认行为;
因此,使用原生js时,若要阻止默认行为,最好还是用event.preventDefault(针对非IE)或event.returnValue=false(针对IE)来设定。
若使用的是jquery,return false 即会阻止默认行为,也会阻止事件的冒泡。在jquery中,一般使用return false.
原生js中stopPropagation,preventDefault,return false的区别的更多相关文章
- 【前端】stopPropagation, preventDefault, return false的区别
e.stopPropagation()阻止事件冒泡或者捕获 因为事件可以在各层级的节点中传递, 不管是冒泡还是捕获, 有时我们希望事件在特定节点执行完之后不再传递, 可以使用事件对象的 stopPro ...
- stopPropagation(), preventDefault() , return false 事件
因为有父, 子节点同在, 因为有监听事件和浏览器默认动作之分. 使用 JavaScript 时为了达到预期效果经常需要阻止事件和动作执行. 一般我们会用到三种方法, 分别是 stopPropagati ...
- preventDefault()、stopPropagation()、return false 的区别
preventDefault() e.preventDefault()阻止浏览器默认事件 stopPropagation() e.stopPropagation()阻止冒泡 return false ...
- e.preventDefault() e.stopPropagation()和return false的区别
e.preventDefault(); //阻止事件的默认行为,比如a标签的转向,但不阻止事件的冒泡传播e.stopPropagation() //阻止事件的冒泡传播,但不阻止其默认行为returne ...
- js中fn()和return fn()的区别
参考文章:http://www.jb51.net/article/87977.htm 这文章中没有讲明白,其实只要把文章里的代码加和不加return调试一下就知道是怎么回事了. var i = 0; ...
- js 阻止事件冒泡和默认行为 preventDefault、stopPropagation、return false
preventDefault: preventDefault它是事件对象(Event)的一个方法,作用是取消一个目标元素的默认行为.既然是说默认行为,当然是元素必须有默认行为才能被取消,如果元素本身就 ...
- event.stopPropagation()、event.preventDefault()与return false的区别
做小demo时经常用到return false来取消默认事件,但一直不是很懂它和preventDefault()等的区别,今天查了查文档和大神们的博客,在这里对相关知识点做一个总结 首先开门见山,总结 ...
- How to correctly use preventDefault(), stopPropagation(), or return false; on events
How to correctly use preventDefault(), stopPropagation(), or return false; on events I’m sure this h ...
- js中的preventDefault与stopPropagation详解
本篇文章主要是对js中的preventDefault与stopPropagation进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 首先讲解一下js中preventDefault和stopP ...
随机推荐
- Data Center Manager Leveraging OpenStack
这是去年的一个基于OpenStack的数据中心管理软件的想法. Abstract OpenStack facilates users to provision and manage cloud ser ...
- 【HEVC帧间预测论文】P1.4 Motion Vectors Merging: Low Complexity Prediction Unit Decision
Motion Vectors Merging: Low Complexity Prediction Unit Decision Heuristic for the inter-Prediction o ...
- Oracle EXPDP and IMPDP
一.特点 • 可通过 DBMS_DATAPUMP 调用 • 可提供以下工具: – expdp – impdp – 基于 Web 的界面 • 提供四种数据移动方法: – 数据文件复制 – 直接路径 – ...
- 阿里云首次安装和部署nginx
1.执行yum命令安装依赖 yum -y install pcre* yum -y install openssl* 2.下载nginx //如果没有安装wget,下载已编译版本 yum instal ...
- spring-3-AOP
自定义注解类 1.定义注解类 package anno; import java.lang.annotation.ElementType; import java.lang.annotation.Re ...
- Centos7 使用firewall管理防火墙
一.Centos7使用firewall的管理防火墙 1.firewalld基本使用 启动:systemctl start firewalld 关闭:systemctl stop firewalld 状 ...
- 使用Docker compose编排Laravel应用
前言 Laravel官方开发环境推荐的是Homestead(其实就是一个封装好的Vagrant box),我感觉这个比较重,于是自己用Docker compose编排了一套开发环境,在这里分享下. 环 ...
- hdu4407Sum(容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=4407 Sum Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 条款14:在资源管理类中心copying行为(Think carefully about copying behavior in resource-manage classes)
NOTE: 1.复制RAII 对象必须一并赋值它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 2.普遍而常见的RAII class copying 行为是: 抑制c ...
- 微信小程序request请求动态获取数据
微信小程序开发文档链接 1 后台代码: clickButton:function(){ var that = this; wx.request({ url: 'http://localhost:909 ...