CSS + JavaScript 利用display:none/block 構造快顯功能表

來源:互聯網
上載者:User

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<style type="text/css">
*{margin:0px; padding:0px; border=0; list-style:none; font-family:verdana;}

#nav{font-size:10pt}

#nav a{text-decoration:none}
#nav a:link{color:#999}
#nav a:visited{color:#999}
#nav a:hover{color:black}
#nav a:active{color:#999}

#nav li{width:120px; padding:4px; border:solid 1px; margin:3px; text-align:center; position:relative} /* 整個一級菜單占的長度120+4+1+3=128px */

#nav li ul{display:none; position:absolute; left:129px; top:-4px; background:#fff} /*padding:4px,利用display:none隱藏菜單,background:#fff用於填充空隙,否則IE中有問題*/

/*important for ff */
#nav li ul li a{display:inline-block; background:#eee; border:solid 1px; width: 120px} /*用display將a轉化為塊元素,為右彈效果做準備*/
#nav li ul li a{display:inline-block}
/*設定帶連結子功能表的外觀*/
#nav li ul li a:link{color:black}
#nav li ul li a:visited{color:black}
#nav li ul li a:hover{position:relative; left:8px} /*右彈的效果*/
#nav li ul li a:active{color:black}

#nav li:hover ul, #nav li.over ul{display:block} /*顯示被隱藏的菜單*/

</style>
<script type="text/javascript">
var a = function()
{
var nav = document.getElementById('nav');
var lis = nav.childNodes;
for(var i = 0;i<lis.length;i++)
{
if(lis[i].nodeName=='LI')
{
var childLi = lis[i];
childLi.onmouseover = function(){this.className='over';}
childLi.onmouseout = function(){this.className='';}
}
}
}
window.onload = a;
</script>
</head>
<body>
<ul id="nav">
<li><a href="#1">產品介紹</a>
<ul>
<li><a href="#1">Link</a></li>
</ul>
</li>
<li>產品介紹
<ul>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
</ul>
</li>
<li>產品介紹
<ul>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
</ul>
</li>
<li>產品介紹
<ul>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
</ul>
</li>
<li>產品介紹
<ul>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
<li><a href="#1">Link</a></li>
</ul>
</li>
</ul>
</body>
</html>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.