手动实现bind函数(jquery绑定点击事件的方法)

本文目录
jquery绑定点击事件的方法
jQuery绑定点击事件可以使用其绑定事件函数
jquery中四个事件绑定方式(bind,live,delegate,on)
1、bind()
多事件处理:
1.利用空格分隔多事件,例如 $(selector).bind("click dbclick mouseout",data,function);
2.利用大括号灵活定义多事件,例如 $(selector).bind({event1:function, event2:function, ...})
3.空格相隔方式:绑定较为死板,不能给事件单独绑定函数,适合处理多个事件调用同一函数情况;
简要描述
bind()向匹配元素添加一个或多个事件处理器。
使用方式
$(selector).bind(event,data,function)
event:必需项;添加到元素的一个或多个事件,例如 click,dblclick等;
单事件处理:例如 $(selector).bind("click",data,function);
大括号替代方式:绑定较为灵活,可以给事件单独
绑定函数;
data:可选;需要传递的参数;
function:必需;当绑定事件发生时,需要执行的函数;
适用Jquery版本
适用所有版本,但是根据官网解释,自从jquery1.7版本以后bind()函数推荐用on()来代替。
2、live()
简要描述
live() 向当前或未来的匹配元素添加一个或多个事件处理器;
使用方式
$(selector).live(event,data,function)
event:必需项;添加到元素的一个或多个事件,例如 click,dblclick等;
单事件处理:例如 $(selector).live("click",data,function);
多事件处理:1.利用空格分隔多事件,例如 $(selector).live("click dbclick mouseout",data,function);
2.利用大括号灵活定义多事件,例如 $(selector).live({event1:function, event2:function, ...})
3.空格相隔方式:绑定较为死板,不能给事件单独绑定函数,适合处理多个事件调用同一函数情况;
大括号替代方式:绑定较为灵活,可以给事件单独绑定函数;
data:可选;需要传递的参数;
function:必需;当绑定事件发生时,需要执行的函数;
适用Jquery版本
jquery1.9版本以下支持,jquery1.9及其以上版本删除了此方法,jquery1.9以上版本用on()方法来代替。
3、delegate()
简要描述
delegate() 为指定的元素(被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。使用 delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。
使用方式
$(selector).delegate(childSelector,event,data,function)
childSelector: 必需项;需要添加事件处理程序的元素,一般为selector的子元素;
event:必需项;添加到元素的一个或多个事件,例如 click,dblclick等;
单事件处理:例如 $(selector).delegate(childselector,"click",data,function);
多事件处理:1.利用空格分隔多事件,例如 $(selector).delegate(childselector,"click dbclick mouseout",data,function);
2.利用大括号灵活定义多事件,例如 $(selector).delegate(childselector,{event1:function, event2:function, ...})
3.空格相隔方式:绑定较为死板,不能给事件单独绑定函数,适合处理多个事件调用同一函数情况;
大括号替代方式:绑定较为灵活,可以给事件单独绑定函数;
data:可选;需要传递的参数;
function:必需;当绑定事件发生时,需要执行的函数;
适用Jquery版本
jquery1.4.2及其以上版本;
4、on()
简要描述
on() 为指定的元素,添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。使用 on() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。
使用方式
$(selector).on(event,childselector,data,function)
event:必需项;添加到元素的一个或多个事件,例如 click,dblclick等;
单事件处理:例如 $(selector).on("click",childselector,data,function);
多事件处理:1.利用空格分隔多事件,例如 $(selector).on("click dbclick mouseout",childseletor,data,function);
2.利用大括号灵活定义多事件,例如 $(selector).on({event1:function, event2:function, ...},childselector);
3.空格相隔方式:绑定较为死板,不能给事件单独绑定函数,适合处理多个事件调用同一函数情况;
大括号替代方式:绑定较为灵活,可以给事件单独绑定函数;
childSelector: 可选;需要添加事件处理程序的元素,一般为selector的子元素;
data:可选;需要传递的参数;
function:必需;当绑定事件发生时,需要执行的函数;
适用Jquery版本
jquery1.7及其以上版本;jquery1.7版本出现之后用于替代bind(),live()绑定事件方式;
四种方式的异同和优缺点
相同点:
1.都支持单元素多事件的绑定;空格相隔方式或者大括号替代方式;
2.均是通过事件冒泡方式,将事件传递到document进行事件的响应;
比较和联系:
1.bind()函数只能针对已经存在的元素进行事件的设置;但是live(),on(),delegate()均支持未来新添加元素的事件设置;
2.bind()函数在jquery1.7版本以前比较受推崇,1.7版本出来之后,官方已经不推荐用bind(),替代函数为on(),这也是1.7版本新添加的函数,同样,可以
用来代替live()函数,live()函数在1.9版本已经删除;
3.live()函数和delegate()函数两者类似,但是live()函数在执行速度,灵活性和CSS选择器支持方面较delegate()差些
4.bind()支持Jquery所有版本;live()支持jquery1.8-;delegate()支持jquery1.4.2+;on()支持jquery1.7+;
总结
如果项目中引用jquery版本为低版本,推荐用delegate(),高版本jquery可以使用on()来代替,以上仅为个人看法
如何手动实现es5中的bind方法详解
前言
this的指向在javascript中一直是个谜一样的存在,但是很多地方又会用到this,所以理解和使用this非常重要,关于this的理解这篇文章不做介绍,因为这篇的目的是改变this的指向。
改变this的指向有三种方法,call,apply,bind。下面先介绍下这三种方法,话不多说了,来一起看看详细的介绍吧
改变this指向
call
var a = {
name:"aaa",
say(type){
console.log(type,this.name);
}
}
a.say("at");//at aaa
var tn = {name:"ttt"};
a.say.call(tn,"tt")//tt ttt
可以看到通过call,say方法中的this指向了tn,传参的方式的列举
apply
var a = {
name:"aaa",
say(type){
console.log(type,this.name);
}
}
a.say("at");
var tn = {name:"ttt"};
a.say.apply(tn,)
可以看到通过apply,say方法中的this指向了tn,传参的方式是数组
bind
bind也能改变this的指向,不过和call,apply不同的地方在于,bind只改变this,不会指向函数
var a = {
name:"aaa",
say(type){
console.log(type,this.name);
}
}
var tn = {name:"ttt"};
var b = a.say.bind(tn);
b();//ttt
bind 改变this,也是能够继承原型链的,看下下面的代码
var to = {name:"to",color:"red"};
function Animal(){
console.log(`name:${this.name}...color:${this.color}`);
}
Animal.prototype.say = function(){
console.log(`say..name:${this.name}...color:${this.color}`);
}
var Cat = Animal.bind(to);
Cat();//name:to...color:red
var cat = new Cat();// name:undefined...color:undefined
cat.say();//say..name:undefined...color:undefined
因为cat是Cat的实例,Cat是改变了this的Animal,所以cat也是Animal的实例,但是this是指向cat的,所以this.name是undefined
实现bind
Function.prototype.bind = function(obj){
const args = Array.prototype.slice.call(arguments,1);//保留bind时的参数
const that = this;
const bound = function(){
const inArgs = Array.prototype.slice.call(arguments);//执行bind的函数时的参数
const newArgs = args.concat(inArgs);//组装参数
that.apply(obj,newArgs);//执行bind的函数
}
//继承prototype--寄生组合式继承
function F(){};
F.prototype = that.prototype;
bound.prototype = new F();
return bound;
}
然后执行上面的代码
Cat();//name:to...color:red
var cat = new Cat();//name:to...color:red
cat.say();//say..name:undefined...color:undefined
不过第二行和原生的bind还是有点区别的,这里还是记住了之前的bind的对象,原生的不知道为啥是undefined
面试题
实现es5中的bind方法,使得下面的代码输出success
function Animal(name,color){
this.name = name;
this.color = color;
}
Animal.prototype.say = function(){
return `i am a ${this.color} ${this.name}`
}
const Cat = Animal.bind(null,"cat");
const cat = new Cat("white");
if(cat.say() === ’i am a white cat’ && cat instanceof Cat && cat instanceof Animal){
console.log("success")
}
加上上面的bind实现,咦??没有出现success??为什么?
分析一下代码,bind的第一个参数是null??null的时候应该默认为this,修改代码如下
Function.prototype.bind = function(obj){
const args = Array.prototype.slice.call(arguments,1);//保留bind时的参数
const that = this;
const bound = function(){
const inArgs = Array.prototype.slice.call(arguments);//执行bind的函数时的参数
const newArgs = args.concat(inArgs);//组装参数
const bo = obj || this;
that.apply(bo,newArgs);//执行bind的函数
}
//继承prototype--寄生组合式继承
function F(){};
F.prototype = that.prototype;
bound.prototype = new F();
return bound;
}
输出success
完美~~~
撒花~~~
总结

更多文章:
数组c语言例题(一道c语言题,关于数组的:有如下程序 main() {int n[5]={0,0,0),i)
2026年8月22日 18:15
crontab每月一断时间执行(crontab怎么设置在每个月的某一天不执行,其他时间都执行)
2025年9月24日 22:15
如何使用u盘安装linux系统(怎么用u盘安装过linux详细步骤!)
2026年1月10日 17:45
jdbctemplate jar包(我写了一个java程序(有main函数的那种),我现在想让这个程序每天定时自动运行一遍,望高手指点)
2025年7月30日 12:45
中俄联合巡航后,俄军图-95战略轰炸机首次降落中国?轰-20、轰-6、歼轰-20
2026年8月31日 00:30
reserve a table(reserve a table for 3 at 6 是啥意思)
2026年4月4日 16:45
在哪儿打开curl v(windows curl工具怎么用)
2025年11月3日 18:15
zabbix中文官网(The requested URL was not found on this server是什么意思)
2026年1月11日 12:45
git服务器硬件配置要求(做服务器,对电脑的配置要求是多少)
2026年1月1日 11:15
js转换时间格式(js中怎么将时间戳转换为 yyyy-mm-dd格式)
2025年8月2日 13:00
es6promise的理解(es6 promise的catch 和 then 的区别认识)
2025年9月27日 23:15
代码编辑器排行2022(电脑文本编辑器推荐_文本编辑器排行)
2026年1月4日 12:00









