-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddEventListener.html
More file actions
45 lines (45 loc) · 1.63 KB
/
addEventListener.html
File metadata and controls
45 lines (45 loc) · 1.63 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>addEventListener</title>
</head>
<body>
<button id="myBtn1">试一试</button><hr>
<button id="myBtn2">试二试</button><hr>
<button id="myBtn3">试三试</button><hr>
<div id="text" style="width: 100px; height: 50px; background-color: yellow">试四试</div>
<p id="demo"></p>
<script>
document.getElementById('myBtn1').addEventListener('click', displayDate);
document.getElementById('myBtn2').addEventListener('click', function() {
alert('Hello world!');
});
document.getElementById('myBtn3').addEventListener('click', myFunction_1);
document.getElementById('myBtn3').addEventListener('click', myFunction_2);
document.getElementById('text').addEventListener('mouseover', function(){
myFunction_3(this);
});
document.getElementById('text').addEventListener('mouseout', function() {
myFunction_4(this);
});
function displayDate() {
document.getElementById('demo').innerHTML = Date();
}
function myFunction_1() {
alert('这是我的第一个函数');
}
function myFunction_2() {
alert('这是我的第二个函数');
}
function myFunction_3(obj) {
obj.style.backgroundColor = 'black';
}
function myFunction_4(obj) {
obj.style.backgroundColor = 'yellow';
}
</script>
</body>
</html>