forked from bethrobson/Head-First-JavaScript-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletWithClosures.html
46 lines (45 loc) · 1.03 KB
/
letWithClosures.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
<!doctype html>
<html>
<head>
<title>Let, scope, and closures</title>
<meta charset="utf-8">
<style>
div {
padding: 5px;
margin: 5px;
border: 1px solid black;
background-color: lightgray;
width: 100px;
cursor: pointer;
}
</style>
<script src="https://traceur-compiler.googlecode.com/git/bin/traceur.js"
type="text/javascript"></script>
<script src="https://traceur-compiler.googlecode.com/git/src/bootstrap.js"
type="text/javascript"></script>
<script>
traceur.options.experimental = true;
</script>
<script type="module">
window.onload = function() {
var button = document.getElementById("button");
button.onclick = function() {
var body = document.querySelector("body");
for (let i = 0; i < 3; i++) {
var div = document.createElement("div");
div.id = "div" + i;
div.innerHTML = "This is div " + i;
div.onclick = function() {
console.log("You just clicked div " + i);
};
body.appendChild(div);
}
};
};
</script>
</head>
<body>
<h1>let, scope, and closures</h1>
<button id="button">click me</button>
</body>
</html>