perventDefault, stopPropagation, stopImmediatePropagation 三者的区别
event有三种特别容易混淆的方法, 用来阻止默认事件的发生
1. e.preventDefault();
2. e.stopPropagation();
3. e.stopImmediatePropagation();
首先看这三个在MDN上的介绍
preventDefault: 如果当前event.cancelable属性为true, 则取消当前默认的动作, 但不阻止当前事件进一步传播.
stopPropagation: 阻止当前冒泡或者捕获阶段的进一步传播 .
stopImmediatePropagation: 阻止调用相同事件的其他监听器
1. e.preventDefault
下面一个例子, 当点击一个form中的 submit按钮提交表单时, 如果使用了 e.preventDefault , 就可以阻止表单提交
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单</title>
</head>
<body>
<form id="myForm" action="www.baidu.com" method="post">
<div>
<label for="name">姓名:</label>
<input type="text" id="name" />
</div>
<div>
<label for="mail">电子邮箱:</label>
<input type="email" id="mail" />
</div>
<div>
<label for="msg">消息:</label>
<textarea id="msg"></textarea>
</div> <div class="button">
<button type="submit">发送你的消息</button>
</div>
</form>
</body>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
$('#myForm').on('submit', function(e) {
e.preventDefault(); // 什么都不会发生
});
</script>
</html>
此时使用e.preventDefault就能确保表单不会被提交, 但是他不能用来阻止来自冒泡阶段的submit或者点击事件, 下面两种方法就是为了解决这个问题的
注: 如果 Event 对象的 cancelable 属性是 fasle,那么就没有默认动作,或者不能阻止默认动作。无论哪种情况,调用该方法都没有作用。
2. e.stopPropagation
stopPropagation就是保证当前事件不在进一步冒泡
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>冒泡</title>
<style type="text/css">
.box1 {
height: 400px;
width: 400px;
background: #f66;
}
.box2 {
height: 300px;
width: 300px;
background: #6f6;
}
.box3 {
height: 200px;
width: 200px;
background: #66f;
}
</style>
</head>
<body>
<div class="box1">
1
<div class="box2">
2
<div class="box3">3</div>
</div>
</div>
</body>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
$('.box1').on('click', function() {
alert('ok');
})
$('.box2').on('click', function(e) {
// e.preventDefault();
e.stopPropagation(); // 点击box2不会反应
})
$('.box3').on('click', function(e) {
// e.preventDefault();
e.stopPropagation(); // 点击box3不会反应
})
</script>
</html>
3. e.stopImmediatePropagation
以上两种方法已经解决了我们在处理事件中90%的问题, 接下来就介绍一种上面两种无法处理的情形
给一个标签添加两个 class, 一个是公共的class: item, 另一个是独有的class: element, 假设这两个class对当前网站的功能都很重要
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div class="container">
<a href="#" class="item element">点击</a>
</div>
</body>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
$('.item').on('click', function(e) {
console.log('item 被点击了');
});
$('.element').on('click', function(e) {
e.preventDefault(); // 此时链接不会跳转
e.stopPropagation(); // 此时事件冒泡被阻止
console.log('element 被点击了');
});
</script>
</html>
当我们点击<a>标签时

这个现象会发生是因为item与element在DOM中是被平等对待的,与a标签点击冒泡到父级div不同, 我们点击同时出发了item与element的事件, 此时使用stopPropagation无法阻止这种事件
$('.element').on('click', function(e) {
e.preventDefault(); // 此时链接不会跳转
e.stopImmediatePropagation(); // item的点击事件将被阻止
console.log('element 被点击了');
});
$('.item').on('click', function(e) {
console.log('item 被点击了');
});
perventDefault, stopPropagation, stopImmediatePropagation 三者的区别的更多相关文章
- string、Empty和null三者的区别
string.Empty和null三者的区别 本文转自 http://www.bitscn.com/pdb/dotnet/201003/181883.html 时间:2010-03-01 00:00 ...
- android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别
android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...
- 菜鸟,大牛和教主三者的区别(转自hzwer)
菜鸟,大牛和教主,三者的区别 对菜鸟来说题目有三种:会算法且能AC的,会算法但不能AC的,不会做的 对大牛来说题目有两种:会做的,不会做的 对教主来说题目有两种:能AC的,数据有错的 菜鸟提交WA了, ...
- UIColor,CGColor,CIColor三者的区别和联系
UIColor,CGColor,CIColor三者的区别和联系((转)) 最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清 ...
- /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别
原文地址: /storage/sdcard, /sdcard, /mnt/sdcard 三者的区别 - petercao - 博客园 http://www.cnblogs.com/bluestorm/ ...
- jQuery中,$.extend,$obj.extend和$.fn.extend三者的区别
jQuery中,$.extend,$obj.extend和$.fn.extend三者的区别 现在做的一个项目,所使用的框架是基于jQuery扩展的,于是平时学了一下jQuery,了解到了它的扩展函数: ...
- String,StringBuilder,StringBuffer三者的区别
参考 String,StringBuilder,StringBuffer三者的区别 这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面. 1.运行速度 首先说运行速度,或者说是执行速 ...
- this指向和apply,call,bind三者的区别
一.前言 this指向,apply,call,bind的区别是一个经典的面试问题,同时在项目中会经常使用到的原生的js方法.同时也是ES5中的众多坑的一个.ES6中可能会极大的避免了this产生的错误 ...
- ava、Python和PHP三者的区别
Java.Python和PHP三者的区别 2017年07月15日 22:09:21 书生_AABB 阅读数:18994 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
随机推荐
- Gym - 100989M(dp)
George met AbdelKader in the corridor of the CS department busy trying to fix a group of incorrect e ...
- JS(JQEERY) 获取JSON对象中的KEY VALUE
var json= { "Type": "Coding", "Height":100 }; for (var key in json) { ...
- Alpha冲刺3
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9971198.html 作业博客:https://edu.cnblogs.com/campus/ ...
- <Yarn><Node Labels>
Go through official docs For the official docs of Yarn node label, plase see here. Overview Node lab ...
- python 读写、创建 文件的方法(必看)
python 读写.创建 文件的方法(必看) 更新时间:2016年09月12日 10:26:41 投稿:jingxian 我要评论下面小编就为大家带来一篇python 读写.创建 文件的方法(必看). ...
- 莫烦tensorflow(1)-训练线性函数模型
import tensorflow as tfimport numpy as np #create datax_data = np.random.rand(100).astype(np.float32 ...
- svg相关
1.指定点缩放公式 translate(-centerX*(factor-1), -centerY*(factor-1)) scale(factor)
- FZU 2273 Triangles 第八届福建省赛 (三角形面积交 有重边算相交)
Problem Description This is a simple problem. Given two triangles A and B, you should determine they ...
- extjs技术
转载:http://www.cnblogs.com/willick/p/3168809.html 转载 :http://www.cnblogs.com/youring2/archive/2013/08 ...
- Java 经典面试题 —— 性能
1. 性能 String.StringBuffer 与 StringBuilder 两个字符串相加,str1+str2,相当于执行: StringBuilder strBuilder1 = new S ...