国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js,jq,css多方面實現簡易下拉菜單功能

2019-11-19 16:35:14
字體:
來源:轉載
供稿:網友

效果圖預覽

這里寫圖片描述

一 、css實現

html代碼部分

<!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title>html+css下拉菜單</title>  <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> </head> <body>  <ul class="menu">   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單一</a>    <ul>     <li>內容一</li>     <li>內容一</li>     <li>內容一</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單二</a>    <ul>     <li>內容二</li>     <li>內容二</li>     <li>內容二</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單三</a>    <ul>     <li>內容三</li>     <li>內容三</li>     <li>內容三</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單四</a>   </li>  </ul> </body></html>

css部分

*{ padding: 0; margin: 0;}a{ text-decoration: none; color: #000;}ul,li{ list-style: none;}.menu{ margin: 50px auto; width: 500px; height: 35px; background-color: #ccc; text-align: center; line-height: 35px;}.menu li{ float: left; width: 20%; position: relative;}.menu li:hover ul{ display: block;}.menu li a{ display: block; }.menu li a:hover{ background-color: burlywood;}.menu li ul{ display: none; position: absolute;}.menu li ul li{ width: 100%; margin-top: 2px; background-color: darkgray;}.menu li ul li:hover{ cursor: pointer; background-color: chartreuse;}

二、js實現

html和js部分(實現方法一)

<!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title>JS下拉菜單</title>  <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> </head> <body>  <ul class="menu" id="menu">   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單一</a>    <ul>     <li>內容一</li>     <li>內容一</li>     <li>內容一</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單二</a>    <ul class="show">     <li>內容二</li>     <li>內容二</li>     <li>內容二</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單三</a>    <ul class="hide">     <li>內容三</li>     <li>內容三</li>     <li>內容三</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單四</a>   </li>  </ul>  <script type="text/javascript">       window.onload = function(){     function $(id){     return typeof id == "string"?document.getElementById(id):id;     }    var menu_li = $("menu").getElementsByTagName("li");        for(var i = 0; i < menu_li.length; i++){     menu_li[i].onmouseover = function(){      var ss = this.getElementsByTagName("ul")[0];      if(ss != undefined){       ss.style.display = "block";      }     }    }    for(var j = 0; j < menu_li.length; j++){     menu_li[j].onmouseout = function(){      var ssa = this.getElementsByTagName("ul")[0];      if(ssa != undefined){       ssa.style.display = "none";      }          }    }       }           </script> </body></html>

html和js部分(實現方法二)

<!DOCTYPE html><html> <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <style>   * {    padding: 0;    margin: 0;   }   li {    list-style: none;    float: left;   }   #tabCon {    clear: both;   }   #tabCon div {    display: none;   }   #tabCon div.fdiv {    display: block;   }  </style> </head> <body>  <div id="tanContainer">   <div id="tab">    <ul>     <li class="fli">標題一</li>     <li>標題二</li>     <li>標題三</li>     <li>標題四</li>    </ul>   </div>   <div id="tabCon">    <div class="fdiv">內容一</div>    <div>內容二</div>    <div>內容三</div>    <div>內容四</div>   </div>  </div> </body> <script>  function $(id){   return typeof id=="string"?document.getElementById(id):id;  }  var EventUtil = {   addHandler: function(element, type, handler) {    if(element.addEventListener) {     element.addEventListener(type, handler, false);    } else if(element.attachEvent) {     element.attachEvent("on" + type + handler);    } else {     element["on" + type] = handler;    }   },   removeHandler: function(element, type, handler) {    if(element.removeEventListener) {     element.removeEventListener(type, handler, false);    } else if(element.detachEvent) {     element.detachEvent("on" + type + handler);    } else {     element["on" + type] = null;    }   }  }  var tabs = $("tab").getElementsByTagName("li");  var divs = $("tabCon").getElementsByTagName("div");   for(var i = 0; i < tabs.length; i++) {   var set = function() {    change(this);   }   EventUtil.addHandler(tabs[i], "click", set);   //tabs[i].onclick=function(){change(this);}  }  function change(obj) {   console.log(obj);   for(var i = 0; i < tabs.length; i++) {    if(tabs[i] == obj) {console.log(tabs[i]);//     tabs[i].style.display = "block";     divs[i].style.display = "block";    } else {//     tabs[i].style.display = "none";     divs[i].style.display = "none";    }   }  } </script></html>

css部分

*{ padding: 0; margin: 0;}a{ text-decoration: none; color: #000;}ul,li{ list-style: none;}.menu{ margin: 50px auto; width: 500px; height: 35px; background-color: #ccc; text-align: center; line-height: 35px;}.menu li{ float: left; width: 20%; position: relative;}.menu li a{ display: block; }.menu li a:hover{ background-color: burlywood;}.menu li ul{ display: none; position: absolute; left: 0;}.menu li ul li{ width: 100%; margin-top: 2px; background-color: darkgray;}.menu li ul li:hover{ cursor: pointer; background-color: chartreuse;}

三、JQ實現

html和jq部分

<!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title>JS下拉菜單</title>  <link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> </head> <body>  <ul class="menu" id="menu">   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單一</a>    <ul>     <li>內容一</li>     <li>內容一</li>     <li>內容一</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單二</a>    <ul class="show">     <li>內容二</li>     <li>內容二</li>     <li>內容二</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單三</a>    <ul class="hide">     <li>內容三</li>     <li>內容三</li>     <li>內容三</li>    </ul>   </li>   <li>    <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜單四</a>   </li>  </ul>  <script type="text/javascript" src="../../jq/jquery-1.7.2.min.js"></script>  <script type="text/javascript">       $(function(){    $(".menu li").hover(function(){     $(this).children("ul").show();    },function(){     $(this).children("ul").hide();    });   });  </script> </body></html>

css部分

*{ padding: 0; margin: 0;}a{ text-decoration: none; color: #000;}ul,li{ list-style: none;}.menu{ margin: 50px auto; width: 500px; height: 35px; background-color: #ccc; text-align: center; line-height: 35px;}.menu li{ float: left; width: 20%; position: relative;}.menu li a{ display: block; }.menu li a:hover{ background-color: burlywood;}.menu li ul{ display: none; position: absolute; left: 0;}.menu li ul li{ width: 100%; margin-top: 2px; background-color: darkgray;}.menu li ul li:hover{ cursor: pointer; background-color: chartreuse;}

以上就是通過css,js,jq三個方式實現簡易下拉菜單的寫法,有更好的寫法歡迎你的指教。希望大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明光市| 扎赉特旗| 南汇区| 清丰县| 洪江市| 湘潭市| 沿河| 定日县| 色达县| 连云港市| 景洪市| 乌拉特中旗| 苏尼特左旗| 肥乡县| 乐山市| 旬邑县| 泊头市| 通辽市| 修水县| 石泉县| 宁蒗| 华蓥市| 鹰潭市| 长治县| 珲春市| 两当县| 枣阳市| 汶上县| 齐齐哈尔市| 东丰县| 乃东县| 湖北省| 云南省| 蓬莱市| 灵山县| 比如县| 赤水市| 临颍县| 天镇县| 广元市| 黑河市|