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() 描述:为匹配的元素集 ...
随机推荐
- Bean相关
Bean容器初始化: 本地文件是绝对路径,classpath是相对路径.例子如下: Bean配置项: 常用Bean的配置项: (1)id :在整个IOC容器中,这个bean的唯一标识 (2)class ...
- Ubuntu下查看CPU、内存和硬盘详细信息的几个命令
CPU: 型号:grep "model name" /proc/cpuinfo |awk -F ':' '{print $NF}' 数量:lscpu |grep "CPU ...
- 【Uva11762】Race to 1
模拟马尔可夫过程,具体看书. #include<bits/stdc++.h> #define N 1000010 using namespace std; ; void calcprime ...
- vijos P1051 送给圣诞夜的极光
调了好久... #include <cstdio> #include <queue> using namespace std; char a[110*110]; /****** ...
- selenium 警告框处理 (弹窗处理)
在web应用中常常会遇见很多用JavaScript编写的alert .confirm 以及prompt 弹窗,这是就需要driver.switchTo().alert()来选取(定位)警告弹窗.再对弹 ...
- 拒绝平庸——浅谈WEB登录页面设计
用户活跃度是检验产品成功与否的重要指标之一,传统行业的商家极为重视门面的装潢,因为一个好的门面可以聚集人气,招揽更多的顾客.古时候的大户人家院子门口的石狮子或其他的摆件的摆放极为讲究,有一定的风水学说 ...
- 网络大数据分析 -- 使用 ElasticSearch + LogStash + Kibana 来可视化网络流量
https://blog.csdn.net/yeasy/article/details/45332493
- 训练指南 UVALive - 3713 (2-SAT)
layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...
- 洛谷——P2117 小Z的矩阵
P2117 小Z的矩阵 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1,则G(A)等于所有A[i][j]*A[j][i]的和对2取余 ...
- 洛谷——P2958 [USACO09OCT]木瓜的丛林Papaya Jungle
P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...