研究fabricjs中发现,它提供canvas.on('mousemove', hh) 来绑定事件, 提供 canvas.off()来取消绑定事件这样的接口,很是方便,

那我们就不妨探究一下内在的实现原理:

  1. <h1>
  2. The "Post-Link" Function Is The "Link" Function In AngularJS Directives
  3. </h1>
  4.  
  5. <p
  6. bn-using-post-link
  7. bn-using-link
  8. bn-using-fn-only>
  9.  
  10. Look at the console-output; I am logging the link-functions from within the
  11. AngularJS source-code.
  12. <button onclick="cancelEve();">取消事件</button>
  13. </p>
  14.  
  15. <script>
  16. function say(){
  17. console.log('say arguments: ', arguments);
  18. console.log(arguments.length);
  19. for(var i in arguments){
  20. console.log(arguments[i]);
  21. }
  22. }
  23. say(1, 2, 3);
  24. // document.onmousemove = function (ev) {
  25. // console.log('moving', ev);
  26. // for(var i in ev){
  27. // console.log("i: ", i, '-------------:',ev[i]);
  28. // }
  29. // };
  30. function mousemoveHandler() {
  31. console.log('mousemove');
  32. }
  33. function mousemoveHandler2() {
  34. console.log('mousemove 2');
  35. }
  36. document.addEventListener('mousemove', mousemoveHandler);
  37. document.addEventListener('mousemove', mousemoveHandler2);
  38.  
  39. var cancelEve = (function (){
  40. // document.onmousemove = null;
  41. var i = 0;
  42. return function(){
  43. i++;
  44. document.removeEventListener('mousemove', mousemoveHandler);
  45. if(i == 2){
  46. document.removeEventListener('mousemove', mousemoveHandler2);
  47. }
  48. };
  49.  
  50. })();
  51. <script>

  

--------------------------------------------------------------------

通过 addEventListener() 可以对同一个事件绑定多个处理处理函数, 而通过 document.onmousemove = function(){//....}这样好像只能绑定一个处理函数。

取消事件绑定的话,后者只需要 document.onmousemove =  null 就可以了。

而前者,则需要通过  removeEventListener() 这个函数来取消绑定的事件, 需要一个一个取消绑定。

-----------------------------------------------------------

直接通过document.onmousemove = hh 绑定的事件,后面绑定的事件会覆盖掉前面绑定的事件。

  1. function mousemoveHandler() {
  2. console.log('mousemove');
  3. }
  4. function mousemoveHandler2() {
  5. console.log('mousemove 2');
  6. }
  7. document.onmousemove = mousemoveHandler;
  8. document.onmousemove = mousemoveHandler2;
  9. var cancelEve = function (ev) {
  10. document.onmousemove = null;
  11. }

  

javascript 的事件绑定和取消事件的更多相关文章

  1. 学习javaScript必知必会(4)~事件、事件绑定、取消事件冒泡、事件对象

    1.常用的事件: ① onload:页面加载 ② onblur: 失去焦点 onfocus: 获取焦点 ③ onclick:点击 ④ onmouseover:鼠标经过 onmouseout:鼠标离开 ...

  2. js和jQuery中的事件绑定与普通事件

    普通事件,是指直接对元素进行事件注册,然后触发 而事件绑定是将事件注册到元素上 两者区别就是在于普通事件不可以重复添加多个事件,若添加也会覆盖,只会触发其中一个事件(最后注册的那个) 而事件绑定是可以 ...

  3. js课程 5-13 js事件绑定和鼠标事件注意事项有哪些

    js课程 5-13  js事件绑定和鼠标事件注意事项有哪些 一.总结 一句话总结:js代码的灵魂就是改变标签的属性和样式,就这两种. 1.js触发改的东西是哪两样? 属性和样式 2.js如何让页面用标 ...

  4. jQuery使用(七):事件绑定与取消,及自定事件的实现原理

    实例方法: on() one() off() trigger() hover() 一.绑定事件与jQuery事件委托 $(selector).eventType(fn); $(selector).on ...

  5. javascript事件绑定和普通事件的区别

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  6. 事件绑定、取消的二种形式 & call

    <script> //call 函数下的一个方法,call方法第一个参数可以改变函数执行过程中的内部this的指向,call方法第二个参数开始就是原来函数的参数列表. function f ...

  7. JavaScript停止事件冒泡和取消事件默认行为

    功能:停止事件冒泡 function stopBubble(e) { // 如果提供了事件对象,则这是一个非IE浏览器 if ( e && e.stopPropagation ) { ...

  8. JS中事件绑定函数,事件捕获,事件冒泡

    1 事件绑定:事件与函数绑定以及怎么取消绑定 1.1 元素.onclick这种形式,如下: <div id="div1">aaa</div> <scr ...

  9. python 全栈开发,Day55(jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,事件委托(事件代理))

    一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集 ...

随机推荐

  1. shell 通过ANSI转换颜色

    格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[控制码" 如果单纯显示字体颜色可以固定控制码位0m. 格式: echo -e "\033[字背景 ...

  2. 层级数据模板 案例(HierarchicalDataTemplateWindow)

    1.xaml 文件 <Window x:Class="DataTemplate.HierarchicalDataTemplateWindow"        xmlns=&q ...

  3. src2中的alpha融合ROI

    #include <cv.h> #include <highgui.h> int main(int argc, char** argv) { IplImage *src1,*s ...

  4. python模块学习:Iterators和Generators

    转自:http://www.cnblogs.com/zhbzz2007/p/6102695.html 1 迭代器: 迭代器,允许你在一个容器上进行迭代的对象. python的迭代器主要是通过__ite ...

  5. 转载---sql之left join、right join、inner join的区别

    原文地址:http://www.cnblogs.com/pcjim/articles/799302.html sql之left join.right join.inner join的区别 left j ...

  6. NumPy、SciPy 等Python包在Windows下的whl安装包下载

    http://www.lfd.uci.edu/~gohlke/pythonlibs/ 感谢加利福尼亚大学尔湾分校(University of California, Irvine)荧光动力实验室(瞎翻 ...

  7. 通过Eclipse生成可运行的jar包

    本来转自http://www.cnblogs.com/lanxuezaipiao/p/3291641.html 我是个追新潮的人,早早用上了MyEclipse10.最近需要使用Fat jar来帮我对一 ...

  8. 详解cookie与session的区别,讲得最透彻的一篇文章

    在PHP面试中 经常碰到请阐述session与cookie的区别与联系,以及如何修改两者的有效时间. 大家都知道,session是存储在服务器端的,cookie是存储在客户端的,session依赖于c ...

  9. IntelliJ IDEA 常用设置/快捷键

    经常用到 IntelliJ IDEA 编写java,由于不时需要重装系统,所以Mark一下一些基本的设置选项,以便查询,这篇帖子会一直更新,只要有常用的新的设置或者快捷键 一.常用设置 显示代码行号 ...

  10. hdu6059( Trie )

    hdu6059 题意 给定数组 \(A\) ,问有多少对下标 \((i, j, k)\) 满足 \(i < j < k\) 且 \((A[i] \ xor \ A[j]) < (A[ ...