当前位置:首页 > IT技术 > 编程语言 > 正文

JavaScript流程控制语句
2022-01-01 23:11:23

 

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>switch输出星期</title>
</head>
<body>
    <script type="text/javascript">
        var week=new Date().getDay();
        console.log(week);    //0-6
        switch(week){
            case 0:
            document.write("今天是星期日");
            break;    //退出
            case 1:
            document.write("今天是星期一");
            break;
            case 2:
            document.write("今天是星期二");
            break;
            case 3:
            document.write("今天是星期三");
            break;
            case 4:
            document.write("今天是星期四");
            break;
            case 5:
            document.write("今天是星期五");
            break;
            default:
            document.write("今天是星期六");
            break;
        }
    </script>
</body>
</html>
<!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>
    <script>
        /*var age=35;
        if(age<18){
            alert("您还没有成年");
        }*/
        var age=prompt("请输入您的年龄");
        console.log(age);
        if(age<18){
            alert("您还没有成年");
        }else{
            alert("您已成年,请提供您的身份证号");
        }
    </script>
</body>
</html>
<!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>
    <script>
        var password=prompt("请设置您的密码");
        //判断密码的长度,如果不是6位,否则
        if(password.length!=6){
            alert("请输入6位的数字密码");
        }else{
            //如果密码是非数字,否则是数字
            if(isNaN(password)==true){
                alert("密码必须要是数字");
            }else{
                alert("密码设置正确");
            }
        }
    </script>
</body>
</html>
<!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>
    <script type="text/javascript">
        var str="abc123";
        var num=parseInt(str);
        if(num==NaN){    //NaN和任何内容都不相等,包括它本身
            alert(NaN);
        }else if(num==123){
            alert(123);
        }else if(typeof num=="number"){
            alert("num");
        }else{
            alert("str");
        }
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>for</title>
</head>
<body>
    <script type="text/javascript">
        for(var i=1;i<=100;i++){
            document.write(i+'<br/>');        //1
        }
        for(var m=99;m>=1;m-=2){
            console.log(m);    //99 97 3 1
        }
        alert(m);    //-1
    </script>
</body>
</html>

 

本文摘自 :https://www.cnblogs.com/

开通会员,享受整站包年服务立即开通 >