if() else if() else()
alert() 弹出警告框
prompt() 输入框,确定:返回输入信息;取消:返回null
<!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 获取字符串长度
<!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('密码设置成功 密码是:'+pwd); } } </script> </body> </html>
js获取星期:
new Date().getDay()
向浏览器输出内容:
document.write()
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循环里一定要定义循环的条件,不然控制台会报错,代码不会向下执行,页面也不会有任何显示
<!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 适合未知循环次数的循环