if() else if() else()html
alert() 彈出警告框瀏覽器
prompt() 輸入框,肯定:返回輸入信息;取消:返回nullspa
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var age=prompt('請輸入年齡!'); if(age<18){ alert('未成年!'); }else if(age>=18 && age<=60){ alert('ok'); }else{ alert('老啦!') } </script> </body> </html>
str.length 獲取字符串長度3d
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var pwd=prompt('請設置密碼~'); if(pwd.length!=6){ alert('請輸入6位數密碼~'); }else{ if(isNaN(pwd)){ alert('請輸入數字~'); }else{ alert('密碼設置成功\n密碼是:'+pwd); } } </script> </body> </html>
js獲取星期:code
new Date().getDay()htm
向瀏覽器輸出內容:blog
document.write()ip
switch 多條件判斷字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var week=new Date().getDay(); var weekstr=""; switch(week){ case 0: weekstr='日'; break; case 1: weekstr='一'; break; case 2: weekstr='二'; break; case 3: weekstr='三'; break; case 4: weekstr='四'; break; case 5: weekstr='五'; break; case 6: weekstr='六'; break; } document.write("今天是星期"+weekstr); </script> </body> </html>
for循環裏必定要定義循環的條件,否則控制檯會報錯,代碼不會向下執行,頁面也不會有任何顯示get
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> for(var i=1;i++){ alert(1); } </script> </body> </html>
for 適合已知循環次數的循環
while 適合未知循環次數的循環