-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.html
34 lines (32 loc) · 1.18 KB
/
bind.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bind函数</title>
<script type="text/javascript">
window.onload = function(){
function bind(obj,Strevent,callback){
if(obj.addEventListener){
obj.addEventListener(Strevent,callback);
}
else{
obj.attachEven("on"+Strevent,function(){
callback.call(obj);})
//obj.attachEven(Strevent,callback)中callback函数调用的时候
//this指向是window,证明这个函数在底层调用callback的时候是当做一个函数
//来使用的,但是我们想要让this指向obj,所以我们封装了一个额外的匿名函数
//用来包装callback这样我们就可以让callback的this指向我们的obj
}
}
var btn1 = document.getElementById("btn1");
var btn2 = document.getElementById("btn2");
bind(btn1,"click",function(){alert("1")});
bind(btn2,"click",function(){alert("2")});
};
</script>
</head>
<body>
<button id="btn1">按钮1</button>
<button id="btn2">按钮2</button>
</body>
</html>