javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)
原文地址:http://javascript.info/tutorial/bubbling-and-capturing
先给出最终的结论:
Summary
- Events first are captured down to deepest target, then bubble up. In IE<9 they only bubble.
- All handlers work on bubbling stage excepts
addEventListenerwith last argumenttrue, which is the only way to catch the event on capturing stage. - Bubbling/capturing can be stopped by
event.cancelBubble=true(IE) orevent.stopPropagation()for other browsers.
bubbling:
DOM elements can be nested inside each other. And somehow, the handler of the parent works even if you click on it’s child.
The reason is event bubbling.
For example, the following DIV handler runs even if you click a nested tag like EM or CODE:
<div onclick="alert('Div handler worked!')"> |
<em>Click here triggers on nested <code>EM</code>, not on <code>DIV</code></em> |
</div> |
That’s because an event bubbles from the nested tag up and triggers the parent.
The main principle of bubbling states:
After an event triggers on the deepest possible element, it then triggers on parents in nesting order.
For example, there are 3 nested divs:
<!DOCTYPE HTML>
<html>
<body>
<link type="text/css" rel="stylesheet" href="example.css"> <div class="d1">1 <!-- the topmost -->
<div class="d2">2
<div class="d3">3 <!-- the innermost -->
</div>
</div>
</div> </body>
</html>
The bubbling guarantees that click on Div 3 will trigger onclick first on the innermost element 3 (also caled the target), then on the element 2, and the last will be element 1.
The order is called a bubbling order, because an event bubbles from the innermost element up through parents, like a bubble of air in the water.
this and event.target
The deepest element which triggered the event is called the target or, the originating element.
Internet Explorer has the srcElement property for it, all W3C-compliant browsers use event.target. The cross-browser code is usually like this:
var target = event.target || event.srcElement |
When handlers trigger on parents:
event.target/srcElement- remains the same originating element.this- is the current element, the one event has bubbled to, the one which runs the handler.
javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)的更多相关文章
- What is event bubbling and capturing?
What is event bubbling and capturing? 答案1 Event bubbling and capturing are two ways of event propaga ...
- 【转载】浅谈事件冒泡与事件捕获 - javascript 事件代理
原文:https://segmentfault.com/a/1190000000749838 事件冒泡与事件捕获 事件冒泡和事件捕获分别由微软和网景公司提出,这两个概念都是为了解决页面中事件流(事件发 ...
- javascript的事件冒泡,阻止事件冒泡和事件委托, 事件委托是事件冒泡的一个应用。
首先,弄明白js 当中,什么是事件,事件模型在js中是如何设计的.什么是事件冒泡? 什么是“事件冒泡”呢?假设这里有一杯水,水被用某种神奇的方式分成不同颜色的几层.这时,从最底层冒出了一个气泡,气泡会 ...
- 浅谈js的事件冒泡和事件捕获
本文地址:https://www.cnblogs.com/christineqing/p/7607113.html 前言: 这篇文章起源于上次工作上的原因,在事件上出的bug,所以就抽空写出一篇 ...
- javascript中的事件冒泡、事件捕获和事件执行顺序
谈起JavaScript的 事件,事件冒泡.事件捕获.阻止默认事件这三个话题,无论是面试还是在平时的工作中,都很难避免. DOM事件标准定义了两种事件流,这两种事件流有着显著的不同并且可能对你的应用有 ...
- javascript --- 事件冒泡与事件捕获
事件冒泡与事件捕获 事件冒泡和事件捕获分别由微软和网景公司提出,这两个概念都是为了解决页面中事件流(事件发生顺序)的问题.考虑下面这段代码,就不写html->head,body之类的代码了,自行 ...
- JavaScript 进阶教程一 JavaScript 中的事件流 - 事件冒泡和事件捕获
先看下面的示例代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Jav ...
- JavaScript中的事件冒泡?事件传播的解释
注:本文来源 可译网 事件冒泡是你在学习javaScript旅途中遇到的一个术语,它涉及到当一个元素被另一个元素嵌套时调用事件处理的顺序,并且两个元素注册了同一个事件(例如,点击事件). 但是事件冒 ...
- JavaScript事件冒泡和事件委托
JavaScript事件冒泡和事件委托 付建宇 - 2 条评论 接触JavaScript不久,学的东西也不是特别多.小雨就是习惯把平时学到的东西拿出来分享.一方面加强自己的印象,一方面可以让自己的经验 ...
随机推荐
- uploadifive源码学习
一.简介 Uploadify是一个基于JQuery的多文件上传JS组件,高度定制,两个版本可供选择.flash版本在最新的Safari等不再支持flash的浏览器或者一些手机浏览器中就无法正常的加载使 ...
- Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
http://c.biancheng.net/cpp/shell/ 1.打印 2.运算符
- windows下如何快速搭建web.py开发框架
在windows下如何快速搭建web.py开发框架 用Python进行web开发的话有很多框架供选择,比如最出名的Django,tornado等,除了这些框架之外,有一个轻量级的框架使用起来也是非常方 ...
- Java Queue 各种方法的区别
再Java里的某些集合类,其实是实现了多个接口的,所以就会同时又多种方法针对同一种操作,比如LinkedList类. 首先看一下java集合类的继承关系图: 这里简单对其重复的方法做点简单的区分. o ...
- VNC VIEWER的使用集锦
关于颜色深度的问题, 今天用VNC Viewser ,连上去之后,发现色彩可能只有8或者16位 然后修改了 sever的depth,也不好使, 于是,就修改了 client的 colourlevel ...
- 浅谈h5移动端页面的适配问题
一.前言 昨天唠叨了哈没用的,今天说点有用的把.先说一下响应式布局吧,我一直认为响应式布局的分项目,一下布局简单得项目做响应式还是可以可以得.例如博客.后台管理系统等.但是有些会认为响应式很牛逼,尤其 ...
- jQuery的dataTables插件实现中文排序
最近在写Java web. 写JSP的时候发现一个很好玩的插件dataTables.分页.过滤.排序等等手到擒来. 哎哎哎,有点点可惜的是排序这个功能不支持中文.于是网上查查找找,现在把方法整理一下, ...
- 《JS权威指南学习总结--第二章词法结构》
第二章词法结构 内容要点: 一.注释 1. //表示单行注释 2. /*这里是一段注释*/ 3.一般编辑器里加注释是:选中要加注释的语句,按 ctrl+/ 二.直接量 所谓直接量,就是程序中直接使用的 ...
- Ubuntu服务器断网问题解决
原因:dns服务器没有了配置信息. 配置dns服务器 ubuntu 的dns服务器信息,放在 /etc/resolv.conf中, 添加dns服务器地址,如192. ...
- svn rollback: 恢复到上一版本
18:48:32svn的文件版本是168,我想用167的版本覆盖掉168的版本如何搞? 18:52:47先把本地的那个文件用rm命令删掉,然后,使用svn up -r 167 文件路径,UP下来的文件 ...