小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Web前端

 天使之翼 ` 2019-06-29

Js練習(xí)

顯示和隱藏,改變display屬性(點擊查看圖片)

關(guān)鍵代碼:

e.style.display = "block";
e.style.display = "none";

源碼:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>顯示和隱藏</title>
        <script type="text/javascript">
            function showPicture(){
                //普通方式獲得控件實例
                var e = document.getElementById("myimg");
                e.style.display = "block";
            }
            
            function hidePicture(){
                //querySelector是html5增加的
                //id前面得寫#,class前面得寫
                document.querySelector("#myimg").style.display = "none";
                //標(biāo)簽直接寫即可,獲取到第一個img標(biāo)簽
                //document.querySelector("img").style.display = "none";
            }
        </script>
    </head>
    <body>
        <a href="javascript:void(0);"onclick="showPicture()">查看圖片</a>
        <a href="javascript:void(0);"onclick="hidePicture()">隱藏圖片</a>
        <br />
        <img id="myimg" src="http://www.w3school.com.cn/i/eg_mouse.jpg" style="display: none;"  >
    </body>
</html>

querySelector三種方法介紹

鼠標(biāo)滑動更改內(nèi)容 onmouseover

關(guān)鍵:
onmouse事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script type="text/javascript">
            function moveToBlue(){
                var e = document.querySelector("#btn_blue");
                var e2 = document.querySelector("#btn_green");
                var div = document.querySelector("#content");
                e.style.border = "1px solid #ccc";
                e.style.backgroundColor = "white";
                e2.style.border = "none";
                e2.style.backgroundColor = "none";
                div.style.backgroundColor = "blue";
            }
            function moveToGreen(){
                var e = document.querySelector("#btn_blue");
                var e2 = document.querySelector("#btn_green");
                var div = document.querySelector("#content");
                e2.style.border = "1px solid #ccc";
                e2.style.backgroundColor = "white";
                e.style.border = "none";
                e.style.backgroundColor = "none";
                div.style.backgroundColor = "green";
            }
        </script>
    </head>
    <body>
        <div style="width: 100px;height: 50px;">
            <button id="btn_blue" style="width: 45px;"onmousemove="moveToBlue()">藍(lán)色</button>
            <button id="btn_green" style="width: 45px;"onmousemove="moveToGreen()">綠色</button>
            <div id="content" style="background-color: blue;width: auto;height: 50px;"></div>
        </div>

        <br />
        <img id="myimg" src="http://www.w3school.com.cn/i/eg_mouse.jpg" style="display: none;">
    </body>
</html>

時間自動更新

關(guān)鍵代碼:
Js中內(nèi)置了Date對象

  • getFullYear 年
  • getMonth 月
  • getDate 日
  • getHours 小時
  • getMinutes 分鐘
  • getSeconds 秒
  • getDay 星期幾(0-6) 星期日為0
方法 說明
getFullYear() 從 Date 對象以四位數(shù)字返回年份。
getMonth() 從 Date 對象返回月份 (0 ~ 11)。
getDate() 從 Date 對象返回一個月中的某一天 (1 ~ 31)。
getDay() 從 Date 對象返回一周中的某一天 (0 ~ 6)。
getHours() 返回 Date 對象的小時 (0 ~ 23)。
getMinutes() 返回 Date 對象的分鐘 (0 ~ 59)。
getSeconds() 返回 Date 對象的秒數(shù) (0 ~ 59)。
getMilliseconds() 返回 Date 對象的毫秒(0 ~ 999)。
toString() 把 Date 對象轉(zhuǎn)換為字符串。
toTimeString() 把 Date 對象的時間部分轉(zhuǎn)換為字符串。
toDateString() 把 Date 對象的日期部分轉(zhuǎn)換為字符串。
toUTCString() 根據(jù)世界時,把 Date 對象轉(zhuǎn)換為字符串。
toLocaleString() 根據(jù)本地時間格式,把 Date 對象轉(zhuǎn)換為字符串。
toLocaleTimeString() 根據(jù)本地時間格式,把 Date 對象的時間部分轉(zhuǎn)換為字符串。
toLocaleDateString() 根據(jù)本地時間格式,把 Date 對象的日期部分轉(zhuǎn)換為字符串。
 var now = new Date();

 var time =now.getFullYear() + '年' + now.getMonth() + '月'
     + now.getDate() + '日'
     +now.getHours() +'時' + now.getMinutes() +'分' + now.getSeconds() + '秒';
  1. setTomeout(fn, 周期:豪秒): 周期只會執(zhí)行一次
  2. setInterval(fn, 周期:豪秒): 間隔周期一直執(zhí)行

源碼:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>時間自動更新</title>
    </head>
    <body>
        <p></p>
        <script type="text/javascript">
            function setTime() {
                var date = new Date();
                var time = date.toTimeString();
                document.querySelector("p").innerText = time;
            }
            setTime();
            //setIime不能加上括號
            // setInterval(setTime, 1000);
            //加括號得得在外層加雙引號或者單引號
            setInterval("setTime()", 1000);
        </script>
    </body>
</html>

源碼(es拼接字符串):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>

    </head>
    <body>
        <p></p>
        <script type="text/javascript">
            function setTime() {
                var now = new Date();
                // 通過css的選擇符獲得html元素
                var timer1 = document.querySelector('p');
                var year = now.getFullYear();
                var month = now.getMonth() + 1;
                var date = now.getDate();
                var hours = now.getHours();
                var minutes = now.getMinutes();
                var seconds = now.getSeconds();
                //es6中模板字符串,拼接
                timer1.innerText = `${year}年${month}月${date}日${hours}時${minutes}分${seconds}秒`;
            }
            setTime();
            //setIime不能加上括號
            // setInterval(setTime, 1000);
            //加括號得得在外層加雙引號或者單引號
            setInterval("setTime()", 1000);
        </script>
    </body>
</html>

表單

關(guān)鍵代碼:e.checked=true;

全選和反選的實現(xiàn)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script type="text/javascript">
            function selectAll() {
                var hobbys = document.getElementsByName("hobby");
                var btn = document.getElementById("checkall");
                //原生for,推薦使用這一種
                /*
                for (var i = 0; i < hobbys.length; i++) {
                    if (btn.checked) {
                        hobbys[i].checked = true;
                    } else {
                        hobbys[i].checked = false;
                    }

                }
                */
                //使用foreach,使用HBuilderX,在內(nèi)置的瀏覽器會報錯,提示foreach不起作用
                //使用谷歌瀏覽器打開則沒有問題,下面使用lambda也是如此情況,可能是HBuilder的bug
                hobbys.forEach(function(hobby) {
                    //如果全選的是選中,則把全部設(shè)置為選中的狀態(tài),否則設(shè)置為未選中
                    if (btn.checked) {
                        hobby.checked = true;
                    } else {
                        hobby.checked = false;
                    }
                });
                //使用lambda
                /*
                hobbys.forEach((hobby) => {
                    console.log(hobby);
                    if (btn.checked) {
                        hobby.checked = true;
                    } else {
                        hobby.checked = false;
                    }
                });
                */

            }

            function selectRevese() {
                var hobbys = document.getElementsByName("hobby");
                for (var i = 0; i < hobbys.length; i++) {
                    //設(shè)置為另外的狀態(tài)
                    hobbys[i].checked = !hobbys[i].checked;
                }
            }
        </script>
    </head>
    <body>

        <input type="checkbox" name="hobby">讀書<br><br>
        <input type="checkbox" name="hobby">電影<br><br>
        <input type="checkbox" name="hobby">游戲<br><br>
        <input type="checkbox" name="hobby">游泳<br><br>
        <input type="checkbox" name="hobby">寫代碼<br><br>
        <input type="checkbox" id="checkall" onclick="selectAll()">全選 </input>
        <button type="button" onclick="selectRevese()">反選</button>


    </body>
</html>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多