<!doctype html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <title>Document</title>
<style type="text/css">
? ? *{margin:0;
? ? ?padding:0;
? ? ?font-size:13px;
? ? ?list-style:none;}
.menu{width:210px;
? ? ? margin:50px auto;
? ? ? border:1px solid #ccc;}
.menu p{height:25px;
? ? ? ? line-height:25px;
? ? ? ? font-weight:bold;
? ? ? ? background:#eee;
? ? ? ? border-bottom:1px solid #ccc;
? ? ? ? cursor:pointer;
? ? ? ? padding-left:5px;}
.menu div ul{display:none;}
.menu li{height:24px;
? ? ? ? ?line-height:24px;
? ? ? ? ?padding-left:5px;}
</style>
<script type="text/javascript">
window.onload=function(){
? ? ? // 將所有點(diǎn)擊的標(biāo)題和要顯示隱藏的列表取出來
? ? ? ? var titles = document.getElementById("menu").getElementsByTagName("p");
? ? ? ? var contents = document.getElementById("menu").getElementsByTagName("ul");
? ? ?// 遍歷所有要點(diǎn)擊的標(biāo)題且給它們添加索引及綁定事件
? ? ? ? for(var i=0,len=titles.length;i<len;i++){
? ? ? ? ? ? titles[i].index = i;
? ? ? ? ? ? titles[i].onclick = function(){
? ? ? ? ? ? ? ? var ul=contents[this.index];
? ? ? ? ? ? ? ? var state=ul.style.display;
? ? ? ? ? ? ? ? //clear all uldisplay style
? ? ? ? ? ? ? ? //hidden all ul content
? ? ? ? ? ? ? ? for (var j = 0; j <titles.length; j++) {
? ? ? ? ? ? ? ? ? ? contents[j].style.display='none';
? ? ? ? ? ? ? ? ? ? //divs[j].className='mod';
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(state=="block"){
? ? ? ? ? ? ? ? ? ? ul.style.display="";
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ul.style.display="block";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? }
? ? ?// 獲取點(diǎn)擊的標(biāo)題上的索引屬性,根據(jù)該索引找到對應(yīng)的列表
? ? ?// 判斷該列表,如果是顯示的則將其隱藏,如果是隱藏的則將其顯示出來
}
</script>
</head>
<body>
<div id="menu">
<div>
<p>Web前端</p>
<ul style="display:block">
<li>JavaScript</li>
<li>DIV+CSS</li>
<li>jQuery</li>
</ul>
</div>
<div>
<p>后臺(tái)腳本</p>
<ul>
<li>PHP</li>
<li>ASP.net</li>
<li>JSP</li>
</ul>
</div>
<div>
<p>前端框架</p>
<ul>
<li>Extjs</li>
<li>Esspress</li>
<li>YUI</li>
</ul>
</div>
</div>
</body>
</html>