-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOM事件.html
117 lines (102 loc) · 2.43 KB
/
DOM事件.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.head {
font-size: 12px;
padding: 6px 0 0 10px;
}
#login_box {
width: 300px;
height: 150px;
background: #eee;
border: 1px solid #ccc;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -75px;
display: none;
}
#login_box p {
height: 20px;
border-bottom: 1px solid #ccc;
font-size: 12px;
padding: 6px 0 0 5px;
font-weight: bold;
}
#close {
width: 14px;
height: 14px;
background-image: url(img/close.jpg);
background-repeat: no-repeat;
background-size: 14px 14px;
position: absolute;
right: 4px;
top: 6px;
}
</style>
<script>
window.onload = function() {
var login_btn = document.getElementById('login'),
login_box = document.getElementById('login_box'),
close = document.getElementById('close');
// 封装添加事件监听程序
function addEvent(ele, type, hander) {
if (ele.addEventListener) {
ele.addEventListener(type, hander, false);
} else if (ele.attachEvent) {
ele.attachEvent("on"+type, hander);
} else {
ele["on"+type] = hander;
alert("0级");
}
}
// 封装移除事件监听程序
function romoveEvent(ele, type, hander) {
if (ele.removeEventListener) {
ele.removeEventListener(type, hander, false);
} else if (ele.attachEvent) {
ele.detachEvent("on"+type, hander);
} else {
ele["on"+type] = null;
}
}
//封装阻止冒泡函数
function cancelPropagation(){
}
// 显示登录层函数
function showLogin(e) {
event=e||window.event;
login_box.style.display="block";
// 执行代码
}
// 隐藏登录层函数
function hideLogin(e) {
event=e||window.event;
login_box.removeAttribute("style");
// login_box.style="";
// 执行代码
}
//点击登录按钮显示登录层
addEvent(login_btn,"click",showLogin);
// 执行代码
//点击关闭按钮隐藏登录层
addEvent(close,"click",hideLogin);
// 执行代码
}
</script>
</head>
<body>
<div class="head">亲,您好!<input type="button" value="登 录" id="login"></div>
<div id="login_box">
<p>用户登录</p><span id="close"></span>
</div>
</body>
</html>