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() 描述:为匹配的元素集 ...
随机推荐
- 什么是SVC模式【转】
转自:http://blog.csdn.net/jobsss/article/details/7548550 版权声明:本文为博主原创文章,未经博主允许不得转载. ARM 处理器有二十七个寄存器,其中 ...
- pymysql 线程安全pymysqlpool
# -*-coding: utf-8-*- # Author : Christopher Lee # License: Apache License # File : test_example.py ...
- MMSEG 中文分词算法 翻译
算法原文位于:http://technology.chtsai.org/mmseg/ http://www.360doc.com/content/13/0217/15/11619026_2661428 ...
- docker从零开始 存储(二)volumes 挂载
使用volumes 卷是保存Docker容器生成和使用的数据的首选机制.mount binds依赖于主机的目录结构,而卷完全由Docker管理.卷绑定安装有几个优点: 与绑定装入相比,卷更易于备份或迁 ...
- DB2 v9.7官方下载链接
http://blog.sina.com.cn/s/blog_8ea8e9d50102w2s6.html
- Luis创建与发布
首先打开网址https://www.luis.ai,打开后,需要使用你的微软帐户或是公司账户登录至Luis 登陆进入至网站后,会自动显示你的应用,在这里你可以修改和删除你之前已经创建过的应用,如果之前 ...
- ubuntu14.04 安装 pyv8
1. $sudo pip install -v pyv8 Error: pip unicodedecodeerror ‘ascii’ codec can’t decode byte 0xe2 in p ...
- Codeforces 810 A.Straight «A»
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces #445 Div2 D
#445 Div2 D 题意 给出一些字符串,要求构造一个最短的且字典序最小的字符串,使得给出的字符串都为目标字符串的子串,且这些字符串作为子串出现的次数都是最多的,如果不存在目标字符串输出 &quo ...
- java应用高内存占用
在java虚拟机中,内存分为三个代:新生代(New), 老生代(Old).永久代(Perm) 新生代: 新建的对象都存放这里老生代:存放从新生代中迁移过来的生命周期较久的对象.新生代和老生代共同组成了 ...