JavaScript基础 - js基础

2018-11-05  本文已影响0人  莫名ypc

1.就近原则
2.具体性原则
3.重要性原则

JavaScript = ECMAScript + BOM + DOM

ES:JavaScript的语法规范
BOM:浏览器对象模型
DOM:文档对象模型

判断是否是闰年

            var isOk = true;
            window.alert(isOk)
            window.alert(!isOk)
            var name = window.prompt('请输入用户名:')
            if(name != 'null' && name.trim().length > 0){
                window.alert('你好,' + name + '!')
            }else{
                window.alert('大家好!')
            }
            while不确定循环次数
            for 循环的次数不确定
            var flag = true;
            while(flag){
                var yearStr = window.prompt('请输入需要判断的年份:');
                var year = parseInt(yearStr);
                if(year == yearStr && year > 0){
                    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
                        window.alert(year + '是闰年');
                    }else{
                        window.alert(year + '不是闰年');
                    }
                    flag = window.confirm('是否继续?');
                }else{
                    window.alert('请输入有效年份');
                }
            }

车牌限号查询

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #div1{
                margin: 0 auto;
                margin-top: 100px;
                text-align: center;
            }
            #carno{
                width: 400px;
                height: 30px;
                font: 30px/36px;
                text-align: center;
                border: none;
                border-bottom: 1px solid pink;
                outline: none;
            }
            #button1{
                color: white;
                font: 25px/36px;
                width: 90px;
                height: 36px;
                border: none;
                background-color: deepskyblue;
                outline: none;
            }
            #button2{
                color: white;
                font: 25px/36px;
                width: 90px;
                height: 36px;
                border: none;
                background-color: deeppink;
                outline: none;
            }
            #result{
                width: 640px;
                margin: 0 auto;
                text-align: center;
                font: 25px/36px arial;
            }
        </style>
    </head>
    <body>
        <div id="div1">
            <!--<label>请输入车牌号:</label><br />-->
            <input type="text" name="" id="carno" value="" placeholder="输入车牌号"/>
            <input type="button" id="button1" value="查&emsp;询" onclick="showResult()"/>
            <input type="button" id="button2" value="清&emsp;除" onclick="clearHistory()"/>
        </div>
        <br />
        <hr />
        <p id="result"></p>
        
        
        <script type="text/javascript">
            function showResult(){
                var input = document.getElementById('carno');
                var p = document.getElementById('result');
                var carNo = input.value;
                var regex = /^[川渝云贵京津沪][A-Z]\s*[0-9A-Z]{5}$/;
                if(regex.test(carNo)){
                    var digitStr = lastDigit(carNo);
                    if (digitStr){
                        var digit = parseInt(digitStr);
                        var day = new Date().getDay();
                        if(digit % 5 == day || digit % 5 == day - 5){
                            newStr = carNo + '今日限行!<br>' + p.innerHTML;
                            p.innerHTML = newStr;
                        }else{
                            newStr = carNo + '今日不限行!<br>' + p.innerHTML;
                            p.innerHTML = newStr;
                        }
                    }else{
                        newStr = carNo + '不是有效车牌!<br>' + p.innerHTML;
                            p.innerHTML = newStr;
                    }
                }else{
                    newStr = carNo + '不是有效车牌!<br>' + p.innerHTML;
                            p.innerHTML = newStr;
                }
                input.value = '';
            }
            
            function lastDigit(str){
                for(var index = str.length - 1; index >= 0; index -= 1){
                    var digitStr = str[index];
                    if(digitStr >= '0' && digitStr <= '9'){
                        return digitStr;
                    }
                }
                return null;
            }
            
            function clearHistory(){
                var c = document.getElementById('result');
                c.innerHTML = '';
            }

        </script>
    </body>
</html>

上一篇下一篇

猜你喜欢

热点阅读