javascript 的事件绑定和取消事件
研究fabricjs中发现,它提供canvas.on('mousemove', hh) 来绑定事件, 提供 canvas.off()来取消绑定事件这样的接口,很是方便,
那我们就不妨探究一下内在的实现原理:
- <h1>
- The "Post-Link" Function Is The "Link" Function In AngularJS Directives
- </h1>
- <p
- bn-using-post-link
- bn-using-link
- bn-using-fn-only>
- Look at the console-output; I am logging the link-functions from within the
- AngularJS source-code.
- <button onclick="cancelEve();">取消事件</button>
- </p>
- <script>
- function say(){
- console.log('say arguments: ', arguments);
- console.log(arguments.length);
- for(var i in arguments){
- console.log(arguments[i]);
- }
- }
- say(1, 2, 3);
- // document.onmousemove = function (ev) {
- // console.log('moving', ev);
- // for(var i in ev){
- // console.log("i: ", i, '-------------:',ev[i]);
- // }
- // };
- function mousemoveHandler() {
- console.log('mousemove');
- }
- function mousemoveHandler2() {
- console.log('mousemove 2');
- }
- document.addEventListener('mousemove', mousemoveHandler);
- document.addEventListener('mousemove', mousemoveHandler2);
- var cancelEve = (function (){
- // document.onmousemove = null;
- var i = 0;
- return function(){
- i++;
- document.removeEventListener('mousemove', mousemoveHandler);
- if(i == 2){
- document.removeEventListener('mousemove', mousemoveHandler2);
- }
- };
- })();
- <script>
--------------------------------------------------------------------
通过 addEventListener() 可以对同一个事件绑定多个处理处理函数, 而通过 document.onmousemove = function(){//....}这样好像只能绑定一个处理函数。
取消事件绑定的话,后者只需要 document.onmousemove = null 就可以了。
而前者,则需要通过 removeEventListener() 这个函数来取消绑定的事件, 需要一个一个取消绑定。
-----------------------------------------------------------
直接通过document.onmousemove = hh 绑定的事件,后面绑定的事件会覆盖掉前面绑定的事件。
- function mousemoveHandler() {
- console.log('mousemove');
- }
- function mousemoveHandler2() {
- console.log('mousemove 2');
- }
- document.onmousemove = mousemoveHandler;
- document.onmousemove = mousemoveHandler2;
- var cancelEve = function (ev) {
- document.onmousemove = null;
- }
javascript 的事件绑定和取消事件的更多相关文章
- 学习javaScript必知必会(4)~事件、事件绑定、取消事件冒泡、事件对象
1.常用的事件: ① onload:页面加载 ② onblur: 失去焦点 onfocus: 获取焦点 ③ onclick:点击 ④ onmouseover:鼠标经过 onmouseout:鼠标离开 ...
- js和jQuery中的事件绑定与普通事件
普通事件,是指直接对元素进行事件注册,然后触发 而事件绑定是将事件注册到元素上 两者区别就是在于普通事件不可以重复添加多个事件,若添加也会覆盖,只会触发其中一个事件(最后注册的那个) 而事件绑定是可以 ...
- js课程 5-13 js事件绑定和鼠标事件注意事项有哪些
js课程 5-13 js事件绑定和鼠标事件注意事项有哪些 一.总结 一句话总结:js代码的灵魂就是改变标签的属性和样式,就这两种. 1.js触发改的东西是哪两样? 属性和样式 2.js如何让页面用标 ...
- jQuery使用(七):事件绑定与取消,及自定事件的实现原理
实例方法: on() one() off() trigger() hover() 一.绑定事件与jQuery事件委托 $(selector).eventType(fn); $(selector).on ...
- javascript事件绑定和普通事件的区别
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- 事件绑定、取消的二种形式 & call
<script> //call 函数下的一个方法,call方法第一个参数可以改变函数执行过程中的内部this的指向,call方法第二个参数开始就是原来函数的参数列表. function f ...
- JavaScript停止事件冒泡和取消事件默认行为
功能:停止事件冒泡 function stopBubble(e) { // 如果提供了事件对象,则这是一个非IE浏览器 if ( e && e.stopPropagation ) { ...
- JS中事件绑定函数,事件捕获,事件冒泡
1 事件绑定:事件与函数绑定以及怎么取消绑定 1.1 元素.onclick这种形式,如下: <div id="div1">aaa</div> <scr ...
- python 全栈开发,Day55(jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,事件委托(事件代理))
一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集 ...
随机推荐
- shell 通过ANSI转换颜色
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[控制码" 如果单纯显示字体颜色可以固定控制码位0m. 格式: echo -e "\033[字背景 ...
- 层级数据模板 案例(HierarchicalDataTemplateWindow)
1.xaml 文件 <Window x:Class="DataTemplate.HierarchicalDataTemplateWindow" xmlns=&q ...
- src2中的alpha融合ROI
#include <cv.h> #include <highgui.h> int main(int argc, char** argv) { IplImage *src1,*s ...
- python模块学习:Iterators和Generators
转自:http://www.cnblogs.com/zhbzz2007/p/6102695.html 1 迭代器: 迭代器,允许你在一个容器上进行迭代的对象. python的迭代器主要是通过__ite ...
- 转载---sql之left join、right join、inner join的区别
原文地址:http://www.cnblogs.com/pcjim/articles/799302.html sql之left join.right join.inner join的区别 left j ...
- NumPy、SciPy 等Python包在Windows下的whl安装包下载
http://www.lfd.uci.edu/~gohlke/pythonlibs/ 感谢加利福尼亚大学尔湾分校(University of California, Irvine)荧光动力实验室(瞎翻 ...
- 通过Eclipse生成可运行的jar包
本来转自http://www.cnblogs.com/lanxuezaipiao/p/3291641.html 我是个追新潮的人,早早用上了MyEclipse10.最近需要使用Fat jar来帮我对一 ...
- 详解cookie与session的区别,讲得最透彻的一篇文章
在PHP面试中 经常碰到请阐述session与cookie的区别与联系,以及如何修改两者的有效时间. 大家都知道,session是存储在服务器端的,cookie是存储在客户端的,session依赖于c ...
- IntelliJ IDEA 常用设置/快捷键
经常用到 IntelliJ IDEA 编写java,由于不时需要重装系统,所以Mark一下一些基本的设置选项,以便查询,这篇帖子会一直更新,只要有常用的新的设置或者快捷键 一.常用设置 显示代码行号 ...
- hdu6059( Trie )
hdu6059 题意 给定数组 \(A\) ,问有多少对下标 \((i, j, k)\) 满足 \(i < j < k\) 且 \((A[i] \ xor \ A[j]) < (A[ ...