javascript事件学习例子

本文演示了几个javascript中进行事件操作的例子。

冒泡型事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 冒泡型事件 </title>
<script language="javascript">
function add(s) {
var div = document.getElementById('display');
div.innerHTML += s;
}
</script>
</head>
<body onclick="add('body<br>')">
<div onclick="add('div<br>')">
<p onclick="add('p<br>')">click me</p>
</div>
<div id="display">
</div>
</body>
</html>

监听函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 监听函数 </title>
<script language="javascript">
window.onload = function() {
var o = document.getElementById('p1');
o.onclick = function() {
alert('我被点击了');
}
}
</script>
</head>
<body>
<p id="p1">click me</p>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> IE的监听函数 </title>
<script>
function fnClick() {
alert('我被点击了');
o.detachEvent('onclick', fnClick);
}
var o;
window.onload = function() {
o = document.getElementById('p1');
o.attachEvent('onclick', fnClick);
}
</script>
</head>
<body>
<p id="p1">click me</p>
</body>
</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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 标准DOM事件监听 </title>
<script>
function fnClick1() {
alert('click1');
//o.removeEventListener('click', fnClick2, false);
}
function fnClick2() {
alert('click2');
}
var o;
window.onload = function() {
o = document.getElementById('p1');
o.addEventListener('click', fnClick1, false);
o.addEventListener('click', fnClick2, false);
}
</script>
</head>
<body>
<p id="p1">click me</p>
</body>
</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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 事件的类型 </title>
<script>
function handle(e) {
e = e || window.event;
var o = document.getElementById('display');
if (e.type == 'click')
o.innerHTML += '你点击了我<br>';
else if (e.type == 'mouseover')
o.innerHTML += '你移动到我上方<br>';
}
window.onload = function() {
var img = document.getElementsByTagName('img')[0];
img.onclick = handle;
img.onmouseover = handle;
}
</script>
</head>
<body>
<img src="01.jpg" />
<div id="display"></div>
</body>
</html>

获取事件的目标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 获取事件的目标 </title>
<script>
function handle(e) {
e = e || window.event;
var target = e.srcElement || e.target;
alert(target.tagName)
}
window.onload = function() {
var img = document.getElementsByTagName('img')[0];
img.onclick = handle;
}
</script>
</head>
<body>
<img src="01.jpg" />
</body>
</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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 鼠标事件 </title>
<script>
function handle(evt) {
evt = evt || window.event;
var o = document.getElementById('display');
o.innerHTML += evt.type + '<br>';
}
window.onload = function() {
var p1 = document.getElementById('p1');
p1.onmousedown = handle;
p1.onmouseup = handle;
p1.onmouseover = handle;
p1.onmouseout = handle;
p1.onclick = handle;
p1.ondblclick = handle;
}
</script>
</head>
<body>
<p id="p1">helloworld</p>
<div id="display"></div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 鼠标button属性 </title>
<script>
function TestClick(event) {
event = event || window.event;
var div1 = document.getElementById('display');
div1.innerHTML += event.button;
}
document.onmousedown = TestClick;
window.onload = TestClick;
</script>
</head>
<body>
<div id="display"></div>
</body>
</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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 键盘事件 </title>
<script>
function handle(event) {
if (window.event) {
event = window.event;
event.charCode = (event.type == "keypress") ? event.keyCode : 0;
}
var div1 = document.getElementById("display");
div1.innerHTML += event.type + ":charCode" + event.charCode + "keyCode:" + event.keyCode + "<br>";
}
window.onload = function() {
var ta = document.getElementsByTagName('textarea')[0];
ta.onkeypress = handle;
ta.onkeydown = handle;
}
</script>
</head>
<body>
<textarea rows="4" cols="50"></textarea>
<div id="display"></div>
</body>
</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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 屏蔽鼠标右键 </title>
<script>
function block(event) {
event = event || window.event;
if (event.button == 2) {
// alert("鼠标右键不可用");
return false;
}
}
document.onmousedown = block;
</script>
</head>
<body>
<p>屏蔽鼠标右键</p>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 屏蔽鼠标右键 </title>
<script>
function block(event) {
if (window.event) {
event = window.event;
event.returnValue = false;
} else {
event.preventDefault();
}
}
document.oncontextmenu = block;
</script>
</head>
<body>
<p>屏蔽鼠标右键</p>
</body>
</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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 伸缩的二级菜单 </title>
<style type="text/css">
/*一级菜单样式*/
#navigation ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
#navigation ul li {
border-bottom: 1px solid #ed9f9f;
}
#navigation ul li a {
display: block;
padding: 5px 5px 5px 0.5em;
text-decoration: none;
border-left: 12px solid #711515;
border-right: 1px solid #711515;
}
#navigation ul li a:link, navigation ul li a:visited {
background-color: #c11136;
color: #fff;
}
#navigation ul li a:hover {
background-color: #990020;
color: #ffff00;
}
/*二级菜单样式*/
#navigation ul li ul {
list-style-type: none;
margin: 0px;
padding: 0px 0px 0px 0px;
}
#navigation ul li ul li {
border-top: 1px solid #ed9f9f;
}
#navigation ul ul li a {
display: block;
padding: 3px 3px 3px 0.5em;
text-decoration: none;
border-left: 28px solid #a71f1f;
border-right: 1px solid #711515;
}
#navigation ul li ul li a:link, #navigation ul li ul li a:visited {
background-color: #e85070;
color: #fff;
}
#navigation ul li ul li a:hover {
background-color: #c2425d;
color: #ff0;
}
#navigation ul li ul.myHide {
display: none;
}
#navigation ul li ul.myShow {
display: block;
}
</style>
<script language="javascript">
window.onload = function() {
var ul = document.getElementById('listUL');
var li = ul.childNodes;
var a;
for (var i = 0; i < li.length; i++) {
if (li[i].tagName == 'LI' && li[i].getElementsByTagName('ul').length) {
a = li[i].firstChild;
a.onclick = change;
}
}
}
function change() {
var div = this.parentNode.getElementsByTagName('ul')[0];
if (div.className == 'myHide')
div.className = 'myShow';
else
div.className = 'myHide';
}
</script>
</head>
<body>
<div id="navigation">
<ul id="listUL">
<li><a href="#">Home</a></li>
<li><a href="#">News</a>
<ul class="myHide">
<li><a href="#">Latest News</a></li>
<li><a href="#">All News</a></li>
</ul>
</li>
<li><a href="#">Wheather</a>
<ul class="myHide">
<li><a href="#">Today's Weather</a></li>
<li><a href="#">Forecast</a></li>
</ul>
</li>
<li><a href="#">Contact Me</a></li>
</ul>
</div>
</body>
</html>