<!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>
</head>
<body>
<div?id="menu">
<div>
<p?style="background:?#f66;color:#fff">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>
<script?type="text/javascript">
????function?$(id){
????????return?typeof?id===?'string'?document.getElementById(id):id;
????}
window.onload?=?function(){
??????//?將所有點(diǎn)擊的標(biāo)題和要顯示隱藏的列表取出來(lái)
????????var?nav?=?$('menu').getElementsByTagName('p');
????????var?list?=?$('menu').getElementsByTagName('ul');
???????
?????//如果列表數(shù)目和內(nèi)容數(shù)目不等
????????if(nav.length!=list.length)
????????????return;
?????//?遍歷所有要點(diǎn)擊的標(biāo)題且給它們添加索引及綁定事件
????????for(var?i=0;i<nav.length;i++){
????????????nav[i].setAttribute('index',i);
????????????nav[i].onclick?=?function(){
????????????????for(var?j=0;j<nav.length;j++){
????????????????????nav[j].style.background='#eee';
????????????????????nav[j].style.color='#000';
????????????????????list[j].style.display='none';
????????????????}
????????????????this.style.background='#f66';
????????????????this.style.color='#fff';
????????????????list[this.getAttribute('index')].style.display="block";
????????????}
????????}
?????//?獲取點(diǎn)擊的標(biāo)題上的索引屬性,根據(jù)該索引找到對(duì)應(yīng)的列表
?????//?判斷該列表,如果是顯示的則將其隱藏,如果是隱藏的則將其顯示出來(lái)
??}
</script>
</body>
</html>
2016-04-03
good好