<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #wrap{ width: 100px; height: 100px; background-color: #3ea3ff; } #btn{ width: 100px; height: 100px; background-color: lawngreen; } </style> </head> <body> <div id="wrap"></div> <div id="btn"></div> <script> let goudan = document.getElementById('wrap'); let dachui = document.getElementById('btn'); goudan.onclick = function () { goudan.innerHTML = 'wrap被點擊了'; }; dachui.onclick = function () { dachui.innerHTML = 'BTN被點擊了'; }; </script> <!-- js版本問題 ECMA = JavaScript Es5 var function Es6 let const function 要麼所有用Es5的規則定義變量,要麼用Es6不可混 定義變量的時候吧全部變量名統一放在前面 關於變量名的規定 -不能使用關鍵詞/保留詞 -儘可能不能使用js裏面已經有意義的變量名 -組成名字的字符最好是數字/字母/_/$四種而且不以數字開頭 -見名知意 數據類型 number 數字 string 字符串 boolean 布爾值 undefined 未定義 null 空 Symbol 惟一 object [] 數組 {} 對象 let x = 10-vue-router; 數字 let x = '10-vue-router'; 字符串 let x = "10-vue-router"; let x = "true"; let x = `456`; 模板字符串 Es6中心的字符串定義方式 let x = true; 布爾值只有這2種 let x = false; let x; let x = undefined; let x = null; ...一系列代碼以後 x = document.getElementById('box'); let x = Symbol(456); ES6新的獨一無二的 let x = Symbol(456); 這2個不一樣,新定義的不可能和之前任何一個同樣 let x = [10-vue-router,true,10-vue-router]; 數組 對象的一種 let y = ['a','卡','10-vue-router']; console.log( y[2] ); >> 10-vue-router 從0開始 let x =[ 10-vue-router [ 'aa', 'bb' ] ]; console.log(x[1][0]) >> aa 對象 let x ={ name:'zy', age:20, marry:true }; console.log(x.age); --> </body> </html>