Day006 - 感性认识JavaScript (2018-11

2018-11-07  本文已影响0人  雨雨雨90

知识点梳理:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style>
            h1{font: 72px/72px arial;}
            #bar{color:red;}
            .foo{color:green;}
            h1{color:blue !important;} /*重要性原则*/
            #timer{
                width: 350px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                color: yellow;
                background-color: blue;
                float: right;
            }
            
        </style>
    </head>
    <body>
        <div id="timer"></div>
        <h1 id="bar" class="foo">Hello, world!</h1>
        
        <button type="button" onclick="shutDown()">关闭</button>
        <button type="button" onclick="openBaidu()">打开百度</button>
        
        <input type="text" name="车牌号" id="carNo" value="" placeholder="请输入您的车牌号:"/>
        <button type="button" onclick='showResult()'>查询</button>
        
        <p id="result"></p>
                    
        
        <script>
            function showTime(){
                // 创建一个列表,用getDay()作为下标取值
                var weekdays = ['日', '一', '二', '三', '四', '五', '六']
                // Date()构造方法,js调用构造方法需要加new
                            var now = new Date(); 
                            // window.alert(now);  // Mon Nov 05 2018 14:15:25 GMT+0800 (中国标准时间)
                            var year = now.getFullYear();
                            var month = now.getMonth() + 1;  //月份返回的是0-11
                            var date = now.getDate();
                            var hour = now.getHours();
                            var minute = now.getMinutes();
                            var second = now.getSeconds();
                            var day = now.getDay();  // getDay()返回星期对象,0-6(0是星期天)
                            
                //          var timeStr = year + '年' + month + '月' + date + '日' + hour + ':'
                //          + minute + ':' + second;
                            // 三元条件运算,条件成立取冒号前面的值,条件不成立取冒号后面的值
                            // 在python中有类似语法:  self.hp = hp if hp > 0 else 0
                            var timeStr = year + '年' + 
                            (month < 10 ? '0' : '') + month + '月' + 
                            (date < 10 ? '0' : '') + date + '日' + 
                            (hour < 10 ? '0' : '') + hour + ':'
                            + (minute < 10 ? '0' : '') + minute 
                            + ':' + (second < 10 ? '0' : '') + second
                            // + ' 星期' + weekdays[day];
                            + '&nbsp;&nbsp;&emsp;星期<b>' + weekdays[day] + '</b>';
                            // 通过id拿到document对象
                            var div = document.getElementById('timer');
                            // div.textContent = timeStr;
                            div.innerHTML = timeStr;  // 如果带了标签和实体替换符,需要用innerHTML
            }
            // function(){}匿名函数
                        
            showTime();  // 处理第一秒空白期的问题
            /* setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
            setInterval(function, milliseconds, param1, param2, ...)
            code/function   必需。要调用一个代码串,也可以是一个函数。
            milliseconds    必须。周期性执行或调用 code/function 之间的时间间隔,以毫秒计。
            返回值:    返回一个 ID(数字),可以将这个ID传递给clearInterval(),clearTimeout() 以取消执行。*/
            window.setInterval(showTime, 1000);
            
        
            // 打开百度的函数
            // window.open()打开新窗口
            function openBaidu(){
                window.open("https://www.baidu.com")
            }
            
            // 关闭浏览器的函数
            // window.close()关闭浏览器窗口
            function shutDown(){
                if (window.confirm('确定要退出吗?')){
                window.close();
                }
            }
        </script>
    </body>
</html>

机动车车牌号检测:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>成都机动车限行查询</title>
        <style>
            #search {
                width: 640px;
                margin: 0 auto;
                text-align: center;
                margin-top: 150px;
            }
            #carno {
                display: inline-block;
                width: 520px;
                height: 36px;
                font: 36px/36px arial;
                text-align: center;
                vertical-align: middle;
                border: none;
                outline: none;
                border-bottom: 1px dotted darkgray;
            }
            #search input[type=button] {
                width: 80px;
                height: 36px;
                font: 28px/36px arial;
                border: none;
                color: white;
                background-color: red;
                vertical-align: middle;
            }
            #result {
                width: 640px;
                margin: 0 auto;
                text-align: center;
                font: 32px/36px arial;
            }
        </style>
    </head>
    <body>
        <div id="search">
            <input type="text" id="carno" placeholder="请输入车牌号">
            <input type="button" value="查询" onclick="showResult()">
        </div>
        <hr>
        <p id="result"></p>
        <script>
            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) {
                            p.innerHTML += carNo + '今日限行<br>';
                        } else {
                            p.innerHTML += carNo + '今日不限行<br>';
                        }
                    } else {
                        p.innerHTML += carNo + '不是有效的车牌号<br>'; 
                    }
                } else {
                    p.innerHTML += carNo + '不是有效的车牌号<br>'; 
                }
                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;
            }
        </script>
    </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读