夜間模式的開啟與關閉
- 放置點選的按鈕或圖片。
- 定義開關切換函式。
- onclick函式呼叫。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>夜晚模式</title> <script> function mySwitch() { var oBody = document.getElementById("mybody"); var oOnoff = document.getElementById("myonoff"); if (oOnoff.src.match("bulbon")) { oOnoff.src = "http://www.runoob.com/images/pic_bulboff.gif"; oBody.style.background = "black"; oBody.style.color = "yellow" } else { oOnoff.src = "http://www.runoob.com/images/pic_bulbon.gif"; oBody.style.background = "white"; oBody.style.color = "black" } } </script> </head> <body id="mybody"> <img id="myonoff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="40px"> <script> document.write(Date()) </script> </body> </html>
父模板的製作
- 製作網站網頁共有元素的父模板html,包括頂部導航,中間區塊劃分,底部導航,底部說明等。
- 彙總相關的樣式形成獨立的css檔案。
- 彙總相關的js程式碼形成獨立的js檔案。
- 形成完整的base.html+css+js
css檔案:
img { width: 400px; /*設定圖片的大小*/ } div.sa { border-style: solid; /*設定邊距線條格式*/ border-width: 5px; /*設定邊距線條大小*/ border-color: gold; /*設定邊距線條顏色*/ width: 400px; /*盒子大小*/ float: left; /*向左浮動*/ margin: 5px; /*設定外邊距*/ } div.sa img { width: 100%; /*設定盒子內容的縮放*/ heigh: aute; /*aute是自動對齊格式*/ } div.st { text-align: center; padding: 2px; /*設定內邊距*/ } div.sa:hover { border-style: solid; border-width: 5px; border-color: green; } /*設定滑鼠經過時產生的變化*/
js檔案:
function mySwitch() { var oBody = document.getElementById("mybody"); var oOnoff = document.getElementById("myonoff"); if (oOnoff.src.match("bulbon")) { oOnoff.src = "http://www.runoob.com/images/pic_bulboff.gif"; oBody.style.background = "black"; oBody.style.color = "yellow" } else { oOnoff.src = "http://www.runoob.com/images/pic_bulbon.gif"; oBody.style.background = "white"; oBody.style.color = "black" } }
// match()如果檢驗到括號裡的內容就做以下變動
html檔案:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 例項 - 倒置的導航欄</title> <link rel="stylesheet" type="text/css" href="../static/css/lin.css"> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="../static/js/daohang.js"></script> </head> <body id="mybody"> <nav class="navbar navbar-inverse" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">首頁</a> </div> <div> <ul class="nav navbar-nav"> <li><a href="#" onclick="">登陸</a></li> <li><a href="#" onclick="">註冊</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 設定 <b class="caret"></b> </a> <ul class="dropdown-menu"> <li><a href="#">收藏</a></li> <li><a href="#">分享</a></li> <li><a href="#">搜尋</a></li> <li class="divider"></li> <li><a href="#">點贊</a></li> </ul> </li> <li><a href="#">||</a></li> </ul> </div> <div style="float: left"> <img id="myonoff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" style="width:40px" > </div> <div> <input type="text" name="user" id="user" placeholder="請輸入內容"> <input type="button" value="搜尋" class="btn btn-success" onclick=""> </div> </div> </nav> <nav class="navbar navbar-inverse navbar-fixed-bottom" role="navigation"> <ul class="nav navbar-nav"> <li><img src="http://www.gzcc.cn/2016/images/footer1-logo.png"></li> <li style="color: bisque;">版權所有@lin</li> </ul> </nav> <script> document.write(Date()) </script> <div> <div class="sa"> <a href="http://desk.zol.com.cn/"><img src="http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/06/ChMkJlfI162IeK07AAY_egKaTWoAAU6qQHhDUcABj-S342.jpg"></a> <div class="st"><a href="http://desk.zol.com.cn/">愛寵大機密1</a></div> </div> <div class="sa"> <a href="http://desk.zol.com.cn/"><img src="http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/06/ChMkJlfI17KIHB_fAAMRJQRdoxQAAU6qQIhsoEAAxE9307.jpg"></a> <div class="st"><a href="http://desk.zol.com.cn/">愛寵大機密2</a></div> </div> <div class="sa"> <a href="http://desk.zol.com.cn/"><img src="http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/06/ChMkJlfI15yIFHF8AANwCMns-CoAAU6qQFNDZMAA3Ag386.jpg"></a> <div class="st"><a href="http://desk.zol.com.cn/">愛寵大機密3</a></div> </div> <div class="sa"> <a href="http://desk.zol.com.cn/"><img src="http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/06/ChMkJ1fI18uIPl4SAAH7nIUWw9wAAU6qQKoXZ4AAfu0026.jpg"></a> <div class="st"><a href="http://desk.zol.com.cn/">愛寵大機密4</a></div> </div> </div> </body> </html>