jQuery $.on()方法和addEventListener改变this指向

标签(空格分隔): jQuery JavaScript


jQuery $.on()

jq的绑定事件使用$([selector]).on([types], [selector], [data], [fn], [one])方法;解绑事件使用off,但是解绑具体事件时候handler只能是具名函数。

在一个对象中,当我们想要在具名函数中用this访问当前对象的属性,可以从[data]参数传入,然后在具名函数中通过e.data来访问:

var obj = {
options: { a: 1 },
init: function() {
$(window).off('click', this._event);
$(window).on('click', { self: this }, this._event);
},
_event: function(e) {
var self = e.data.self;
console.log(self.options);
}
};

addEventListener

详细内容参见MDN

addEventListener兼容性

1. 通过bind(this)方法

var Foo = function (ele) {
this.name = 'This is foo';
this.onclickA = function (e) {
console.log(this.name); // undefined
};
this.onclickB = function (e) {
console.log(this.name); // This is foo
}; ele.addEventListener('click', this.onclickA, false);
ele.addEventListener('click', this.onclickB.bind(this), false);
}; new Foo(document.body);

2. 通过定制的函数handleEvent去捕获任意类型

var Bar = function (ele) {
this.ele = ele;
this.name = 'This is bar';
this.handleEvent = function (e) {
console.log(this.name);
switch (e.type) {
case 'click':
console.log('Trigger click...');
break;
case 'dblclick':
console.log('Trigger dblclick...');
break;
}
}; ele.addEventListener('click', this, false);
ele.addEventListener('dblclick', this, false);
};
Bar.prototype.remove = function () {
// 你也可以移除这些监听事件
this.ele.removeEventListener('click', this, false);
this.ele.removeEventListener('dblclick', this, false);
}; var bar = new Bar(document.body);
bar.remove();

3. 给EventListener传递一个函数,调用想要访问对应作用域对象的方法

但是这样做绑定的事件成为了匿名函数,是不能取消绑定的。

class SomeClass {
constructor() {
this.name = 'This is a class';
} register() {
const that = this;
window.addEventListener('keydown', function (ev) { return that.foo(ev); });
} foo(e) {
console.log(this.name);
switch (e.keyCode) {
case 65:
console.log('a');
break;
case 13:
console.log('enter');
break;
}
}
} const obj = new SomeClass();
obj.register();

随机推荐

  1. Full Text Search 实现Sort的实现方案

    CREATE TABLE dbo.pageStore( ID int NOT NULL, StoreName varchar(50) NULL, OwnerOccupation varchar(50) ...

  2. Robot Framework(AutoItLibrary库关键字介绍)

    AutoItLibrary库关键字 AutoItLibrary 的对象操作大体上有几大主要部分,Window 操作.Control 操作.Mouse 操作.Process操作.Run 操作.Reg 操 ...

  3. selenium+Python(鼠标和键盘事件)

    本篇总结了 web 页面常用的一些操作元素方法,可以统称为行为事件有些 web 界面的选项菜单需要鼠标悬停在某个元素上才能显示出来(如百度页面的设置按钮). 1 简单操作 1.点击(鼠标左键)页面按钮 ...

  4. Yii “CDbConnection failed to open the DB connection: could not find driver"解决办法

    前言:用Yii框架做项目时,有时会遇到“CDbConnection failed to open the DB connection: could not find driver”这个问题,这个问题通 ...

  5. Java Hashtable 源码(JDK8)

    记录了HashMap也来看看Hashtable吧,最近打算换份实习,所以想看看书回顾一下,不然就快记不得了.....囧啊囧啊,记性太差怎么破??? Hashtable里面的一些变量: Entry< ...

  6. java中的interrupt(),InterruptException和wait(),sleep()

    标题中的几个概念大概设计到线程同步以及线程阻塞这两个概念.线程同步,就是同一时刻,只有一个线程能执行指定的代码:另外一个线程阻塞就是当前线程暂时停在某个位置,等待某个条件成立之后再继续往下面执行.   ...

  7. css样式、js2种方式 控制字符个数,多余的字用省略号代替

    大家好,我是小菜 前端 ,技术不高,正在努力中充电!希望大家多多指教:css <div class="show">大家好,我是小菜 前端 ,技术不高,正在努力中充电!希 ...

  8. CXF生成客户端遇到的问题

    一.CXF环境配置路径错误 1.错误现象 在命令行中输入 wsdl2java -v 检查CXF安装是否正确. 出现错误=> ERROR: Unable to find cxf-manifest. ...

  9. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...

  10. DOM Tree

    DOM Tree   什么是DOM树:网页的所有内容在内存当中,其实是以树形结构存储的 何时创建:当浏览器,读取html中内容的时候,会马上开始创建DOM树. 如何创建: 1.读到HTML的时候还没有 ...