-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddEventListener.html
More file actions
72 lines (70 loc) · 1.67 KB
/
addEventListener.html
File metadata and controls
72 lines (70 loc) · 1.67 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>事件函数仿写</title>
<script>
(function () {
/*function add(obj, event, callback) {
obj.dataset[event] = obj.dataset[event] || [];
console.log(typeof obj.dataset[event]);
obj.dataset[event].push(callback);
console.log(obj.dataset[event]);
obj.addEventListener(event, callback, false);
}*/
function add(obj,types,fn){
obj.listener=obj.listener||{};
obj.listener[types]=obj.listener[types]||[];
obj.listener[types].push(fn);
};
function remove(obj,types,fn) {
obj.removeEventListener(types,fn, false);
delete obj.listener[types];
}
function trigger(obj, types) {
var arr=obj.listener[types]||[];
for(var i=0;i<arr.length;i++){
arr[i]();
}
}
window.onload = function () {
var div=document.getElementsByTagName('div')[0];
add(div,'click',function(){
alert(1);
});
add(div,'click',function(){
alert(2);
});
remove(div,'click');
// trigger(div,'click');
}
})();
</script>
<style>
div{
width:300px;height:300px;background-color:#afa;
}
</style>
<script src='2.0.3/jquery-2.0.3.js'></script>
<script>
$(function(){
$('#d').on('click',function(){
alert(1);
});
$('#d1').on('click',function(){
alert(2);
});
$('#d1').on('mouseover','span',function(){
console.log(3);
});
});
</script>
</head>
<body>
<div>1</div>
<div>2</div>
<div id="d1">
<span>span</span>
</div>
</body>
</html>