-
Notifications
You must be signed in to change notification settings - Fork 0
/
this.html
49 lines (33 loc) · 1.11 KB
/
this.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
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Javascript: this</title>
<script type="text/javascript">
var out;
function print(text) {
out.innerHTML += text + "<br>";
}
function main() {
out = document.getElementById('out');
print(this); // [object Window]
var obj = {a: 'aha'};
obj.f = function() { print(this.a); };
obj.f(); // aha
document.getElementById('click-too').onclick = function() {
print(this); // [object HTMLDivElement]
}
}
function clicked() {
print(this);
}
</script>
</head>
<body onload='main();'>
<!-- In the following line, «this» refers to the div element, in the called «clicked» function, this is the «window» object -->
<div onclick='print(this); this.innerHTML = "clicked"; clicked();' style='background-color:yellow'><b>Click me</b></div><br>
<div id='click-too' style='background-color:pink' ><b>Click me, too</b></div><br>
<div id='out'>
</div>
</body>
</html>