$.on()方法和addEventListener改变this指向
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();
随机推荐
- Unity Github 项目收集
http://gad.qq.com/article/detail/24048 重磅推荐: Github 热门 Unity Assets 查询:http://unitylist.com/browse 最 ...
- ORM框架SQLAlchemy的使用
ORM和SQLAlchemy简介 对象关系映射(Object Relational Mapping,简称ORM),简单的来说,ORM是将数据库中的表与面向对象语言中的类建立了一种对应的关系.然后我们操 ...
- MySQL之存储引擎(表类型)的选择
和大部分的数据库不同,MySQL中有一个存储引擎的概念,用户可以根据数据存储的需求来选择不同的存储引擎.本次博客就来介绍一下MySQL中的存储引擎.MySQL版本 5.7.19. 概述 MySQL的存 ...
- 【idea快捷键】
IntelliJ Idea 常用快捷键列表 idea也是可以切换到eclipse风格的快捷键方式的 在keymap中切换即可 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表 ...
- PostgreSQL Entity Framework 自动迁移
1.依次添加NuGet包 EntityFramework.Npgsql.EntityFramework6.Npgsql,会自动生成一些配置文件,不过缺少数据库驱动的配置节点: <system.d ...
- Oracle ORA-27102的解决办法(out of memory)
原文出自:https://blog.csdn.net/seesun2012 Oracle ORA-27102:out of memory 错误解决办法(简单粗暴,100%能解决内存占用问题) -前置: ...
- A space or line break was encountered after the "@" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following
mvc 控制器调用分布视图出错,("A space or line break was encountered after the "@" character. Only ...
- 爬取地图列表并下载-node.js
var fs = require('fs'); var request = require('request'); var cheerio = require('cheerio'); var url ...
- Python入门-初始函数
今天让我们来初步认识一个在python中非常重要的组成部分:函数 首先,让我们来幻想这样一个场景: 比如说我们现在想要通过社交软件约一个妹子,步骤都有什么? print('打开手机 ') print( ...
- 用CSS隐藏页面元素的5种方法
1.opacity设置一个元素的透明度只是从视觉上隐藏元素,对页面布局还是有影响,读屏软件会原样读出 2.visibility设置为hidden将隐藏我们的元素,对网页布局还是起作用,子元素也会被隐藏 ...