有关JAVASCRIPT的作业

以下是老师叫我做的作业,

1、用户输入一个年份,用来判断是闰年还是平年

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

</head>

<body>

    <h2>判断是平年还是闰年</h2>

    原数据:<input id="old" type="text">

    操作:<input type = "button" value="判断" οnclick="cal()">

    新数据:<input id="new" type="text">

    <script>

        function cal()

        {

            var a = document.getElementById("old").value;

            if((a%4==0)&&(a%100!=0)||(a%400==0))

                document.getElementById("new").value = "闰年";

            else

                document.getElementById("new").value = "平年";

        }

    </script>

</body>

</html>

 嗯哼,就这样,

2、去除字符串的前后的空格

 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

</head>

<body>

    <h2>去除字符串前后的空格</h2>

    原数据:<input id="old" type="text">

    操作:<input type = "button" value="操作" οnclick="cal()">

    新数据:<input id="new" type="text">

    <script>

        function cal()

        {

            var a = document.getElementById("old").value;

            var start, end;

            for(var i = 0;i < a.length; i++)

            {

                if(a[i] == " ")

                {

                    continue;

                }else{

                    start = i;

                    break;

                }

            }

            for(var i = start; i < a.length; i++)

            {

                if(a[i] == "")

                {

                    end = i - 1;

                    break;

                }

                end = i;

            }

            document.getElementById("new").value = a.substring(start,end)

        }

    </script>

</body>

</html>

如下图所示:

 

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页