內部JS:javascript
定義<script>,標籤體內容就是js代碼
外部JS:html
定義<script>,經過src屬性引入外部的js文件
1. <script>能夠定義在html頁面的任何地方。可是定義的位置會影響執行順序。 2. <script>能夠定義多個。
算數運算符java
+,-,*,/,% ...正則表達式
賦值運算符數組
= += -+....瀏覽器
比較運算符app
< >= <= == ===(全等於)dom
三元運算符
? : 表達式
var a = 3;
var b = 4;ecmascript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>99乘法表</title> <style> td{ border: 1px solid; } </style> <script> document.write("<table align='center'>"); //1.完成基本的for循環嵌套,展現乘法表 for (var i = 1; i <= 9 ; i++) { document.write("<tr>"); for (var j = 1; j <=i ; j++) { document.write("<td>"); //輸出 1 * 1 = 1 document.write(i + " * " + j + " = " + ( i*j) +" "); document.write("</td>"); } /*//輸出換行 document.write("<br>");*/ document.write("</tr>"); } //2.完成表格嵌套 document.write("</table>"); </script> </head> <body> </body> </html>
與定時器有關的方式
setTimeout() 在指定的毫秒數後調用函數或計算表達式。
* 參數:
1. js代碼(字符串)或者方法對象(fun)
2. 毫秒值
* 返回值:惟一標識,用於取消定時器
clearTimeout() 取消由 setTimeout() 方法設置的 timeout。函數
setInterval() 按照指定的週期(以毫秒計)來調用函數或計算表達式。
clearInterval() 取消由 setInterval() 設置的 timeout。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自動跳轉</title> <style> p { text-align: center; } #time { color: red; } </style> </head> <body> <p> <span id="time">5</span> 秒以後,自動跳轉到首頁... </p> <script> var time = document.getElementById("time"); var second = 5; function show() { second--; if (second <= 0) { location.href="https://www.baidu.com"; } time.innerHTML = second + ""; } setInterval(show, 1000); </script> </body> </html>
<body> <!-- onclick在屬性值裏耦合度高 --> <img id="light" src="img/off.gif" onclick="fun();"> <img id="light2" src="img/off.gif"> <script> function fun(){ alert('我被點了'); alert('我又被點了'); } function fun2(){ alert('咋老點我?'); } <!-- 這樣比較好 --> //1.獲取light2對象 var light2 = document.getElementById("light2"); //2.綁定事件 light2.onclick = fun2; </script> </body>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>電燈開關</title> </head> <body> <img id="light" src="img/off.gif"> <script> /* 分析: 1.獲取圖片對象 2.綁定單擊事件 3.每次點擊切換圖片 * 規則: * 若是燈是開的 on,切換圖片爲 off * 若是燈是關的 off,切換圖片爲 on * 使用標記flag來完成 */ //1.獲取圖片對象 var light = document.getElementById("light"); var flag = false;//表明燈是滅的。 off圖片 //2.綁定單擊事件 light.onclick = function(){ if(flag){//判斷若是燈是開的,則滅掉 light.src = "img/off.gif"; flag = false; }else{ //若是燈是滅的,則打開 light.src = "img/on.gif"; flag = true; } } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表格全選</title> <style> table{ border: 1px solid; width: 500px; margin-left: 30%; } td,th{ text-align: center; border: 1px solid; } div{ margin-top: 10px; margin-left: 30%; } .out{ background-color: white; } .over{ background-color: pink; } </style> <script> /* 分析: 1.全選: * 獲取全部的checkbox * 遍歷cb,設置每個cb的狀態爲選中 checked */ //1.在頁面加載完後綁定事件 window.onload = function(){ //2.給全選按鈕綁定單擊事件 document.getElementById("selectAll").onclick = function(){ //全選 //1.獲取全部的checkbox var cbs = document.getElementsByName("cb"); //2.遍歷 for (var i = 0; i < cbs.length; i++) { //3.設置每個cb的狀態爲選中 checked cbs[i].checked = true; } } document.getElementById("unSelectAll").onclick = function(){ //全不選 //1.獲取全部的checkbox var cbs = document.getElementsByName("cb"); //2.遍歷 for (var i = 0; i < cbs.length; i++) { //3.設置每個cb的狀態爲未選中 checked cbs[i].checked = false; } } document.getElementById("selectRev").onclick = function(){ //反選 //1.獲取全部的checkbox var cbs = document.getElementsByName("cb"); //2.遍歷 for (var i = 0; i < cbs.length; i++) { //3.設置每個cb的狀態爲相反 cbs[i].checked = !cbs[i].checked; } } document.getElementById("firstCb").onclick = function(){ //第一個cb點擊 //1.獲取全部的checkbox var cbs = document.getElementsByName("cb"); //獲取第一個cb //2.遍歷 for (var i = 0; i < cbs.length; i++) { //3.設置每個cb的狀態和第一個cb的狀態同樣 cbs[i].checked = this.checked; } } //給全部tr綁定鼠標移到元素之上和移出元素事件 var trs = document.getElementsByTagName("tr"); //2.遍歷 for (var i = 0; i < trs.length; i++) { //移到元素之上 trs[i].onmouseover = function(){ this.className = "over"; } //移出元素 trs[i].onmouseout = function(){ this.className = "out"; } } } </script> </head> <body> <table> <caption>學生信息表</caption> <tr> <th><input type="checkbox" name="cb" id="firstCb"></th> <th>編號</th> <th>姓名</th> <th>性別</th> <th>操做</th> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>1</td> <td>令狐沖</td> <td>男</td> <td><a href="javascript:void(0);">刪除</a></td> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>2</td> <td>任我行</td> <td>男</td> <td><a href="javascript:void(0);">刪除</a></td> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>3</td> <td>嶽不羣</td> <td>?</td> <td><a href="javascript:void(0);">刪除</a></td> </tr> </table> <div> <input type="button" id="selectAll" value="全選"> <input type="button" id="unSelectAll" value="全不選"> <input type="button" id="selectRev" value="反選"> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>註冊頁面</title> <style> *{ margin: 0px; padding: 0px; box-sizing: border-box; } body{ background: url("img/register_bg.png") no-repeat center; padding-top: 25px; } .rg_layout{ width: 900px; height: 500px; border: 8px solid #EEEEEE; background-color: white; /*讓div水平居中*/ margin: auto; } .rg_left{ /*border: 1px solid red;*/ float: left; margin: 15px; } .rg_left > p:first-child{ color:#FFD026; font-size: 20px; } .rg_left > p:last-child{ color:#A6A6A6; font-size: 20px; } .rg_center{ float: left; /* border: 1px solid red;*/ } .rg_right{ /*border: 1px solid red;*/ float: right; margin: 15px; } .rg_right > p:first-child{ font-size: 15px; } .rg_right p a { color:pink; } .td_left{ width: 100px; text-align: right; height: 45px; } .td_right{ padding-left: 50px ; } #username,#password,#email,#name,#tel,#birthday,#checkcode{ width: 251px; height: 32px; border: 1px solid #A6A6A6 ; /*設置邊框圓角*/ border-radius: 5px; padding-left: 10px; } #checkcode{ width: 110px; } #img_check{ height: 32px; vertical-align: middle; } #btn_sub{ width: 150px; height: 40px; background-color: #FFD026; border: 1px solid #FFD026 ; } .error{ color:red; } #td_sub{ padding-left: 150px; } </style> <script> /* 分析: 1.給表單綁定onsubmit事件。監聽器中判斷每個方法校驗的結果。 若是都爲true,則監聽器方法返回true 若是有一個爲false,則監聽器方法返回false 2.定義一些方法分別校驗各個表單項。 3.給各個表單項綁定onblur事件。 */ window.onload = function(){ //1.給表單綁定onsubmit事件 document.getElementById("form").onsubmit = function(){ //調用用戶校驗方法 chekUsername(); //調用密碼校驗方法 chekPassword(); //return checkUsername() && checkPassword(); return checkUsername() && checkPassword(); } //給用戶名和密碼框分別綁定離焦事件 document.getElementById("username").onblur = checkUsername; document.getElementById("password").onblur = checkPassword; } //校驗用戶名 function checkUsername(){ //1.獲取用戶名的值 var username = document.getElementById("username").value; //2.定義正則表達式 var reg_username = /^\w{6,12}$/; //3.判斷值是否符合正則的規則 var flag = reg_username.test(username); //4.提示信息 var s_username = document.getElementById("s_username"); if(flag){ //提示綠色對勾 s_username.innerHTML = "<img width='35' height='25' src='img/gou.png'/>"; }else{ //提示紅色用戶名有誤 s_username.innerHTML = "用戶名格式有誤"; } return flag; } //校驗密碼 function checkPassword(){ //1.獲取用戶名的值 var password = document.getElementById("password").value; //2.定義正則表達式 var reg_password = /^\w{6,12}$/; //3.判斷值是否符合正則的規則 var flag = reg_password.test(password); //4.提示信息 var s_password = document.getElementById("s_password"); if(flag){ //提示綠色對勾 s_password.innerHTML = "<img width='35' height='25' src='img/gou.png'/>"; }else{ //提示紅色用戶名有誤 s_password.innerHTML = "密碼格式有誤"; } return flag; } </script> </head> <body> <div class="rg_layout"> <div class="rg_left"> <p>新用戶註冊</p> <p>USER REGISTER</p> </div> <div class="rg_center"> <div class="rg_form"> <!--定義表單 form--> <form action="#" id="form" method="get"> <table> <tr> <td class="td_left"><label for="username">用戶名</label></td> <td class="td_right"> <input type="text" name="username" id="username" placeholder="請輸入用戶名"> <span id="s_username" class="error"></span> </td> </tr> <tr> <td class="td_left"><label for="password">密碼</label></td> <td class="td_right"> <input type="password" name="password" id="password" placeholder="請輸入密碼"> <span id="s_password" class="error"></span> </td> </tr> <tr> <td class="td_left"><label for="email">Email</label></td> <td class="td_right"><input type="email" name="email" id="email" placeholder="請輸入郵箱"></td> </tr> <tr> <td class="td_left"><label for="name">姓名</label></td> <td class="td_right"><input type="text" name="name" id="name" placeholder="請輸入姓名"></td> </tr> <tr> <td class="td_left"><label for="tel">手機號</label></td> <td class="td_right"><input type="text" name="tel" id="tel" placeholder="請輸入手機號"></td> </tr> <tr> <td class="td_left"><label>性別</label></td> <td class="td_right"> <input type="radio" name="gender" value="male" checked> 男 <input type="radio" name="gender" value="female"> 女 </td> </tr> <tr> <td class="td_left"><label for="birthday">出生日期</label></td> <td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="請輸入出生日期"></td> </tr> <tr> <td class="td_left"><label for="checkcode" >驗證碼</label></td> <td class="td_right"><input type="text" name="checkcode" id="checkcode" placeholder="請輸入驗證碼"> <img id="img_check" src="img/verify_code.jpg"> </td> </tr> <tr> <td colspan="2" id="td_sub"><input type="submit" id="btn_sub" value="註冊"></td> </tr> </table> </form> </div> </div> <div class="rg_right"> <p>已有帳號?<a href="#">當即登陸</a></p> </div> </div> </body> </html>