|
@@ -0,0 +1,76 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+</head>
|
|
|
+
|
|
|
+<body>
|
|
|
+ <ul>
|
|
|
+ <li>1</li>
|
|
|
+ <li>2</li>
|
|
|
+ <li>3</li>
|
|
|
+ <li>4</li>
|
|
|
+ <li>5</li>
|
|
|
+ <li>6</li>
|
|
|
+ </ul>
|
|
|
+ <script>
|
|
|
+
|
|
|
+ var aLi = document.getElementsByTagName('li');
|
|
|
+ for (var i = 0; i < aLi.length; i++) {
|
|
|
+ // aLi[i].index = i;
|
|
|
+ // aLi[i].onclick = function () {
|
|
|
+ // console.log(this.index);
|
|
|
+ // }
|
|
|
+ (function(a){
|
|
|
+ aLi[a].onclick = function(){
|
|
|
+ console.log(a);
|
|
|
+ }
|
|
|
+ })(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ // var foo = function(){
|
|
|
+ // console.log("hello");
|
|
|
+ // }
|
|
|
+
|
|
|
+ // foo();
|
|
|
+ // console.log(foo);
|
|
|
+
|
|
|
+ // 立即执行函数
|
|
|
+ // (function(){
|
|
|
+ // console.log("hello");
|
|
|
+ // })();
|
|
|
+
|
|
|
+ // var a = 20;
|
|
|
+ // function foo(){
|
|
|
+ // var a = 10;
|
|
|
+ // function foo2(){
|
|
|
+ // // var a = 30;
|
|
|
+ // console.log(a++);
|
|
|
+ // }
|
|
|
+ // // foo2();
|
|
|
+ // // foo2();
|
|
|
+ // return foo2;
|
|
|
+ // }
|
|
|
+ // var foo3 = foo();
|
|
|
+ // // console.log(foo3)
|
|
|
+ // foo3();
|
|
|
+ // foo3();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 1. 为了防止变量名冲突
|
|
|
+ // 2. 为了防止全局变量污染
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|