Case List Demo:http://codepen.io/tianzi77/pen/ynjvam
First write the structure:
<body><ul id="nav"> <li class="Current">Tianzi</li> <li>Tianzi</li> <li>Tianzi</li></ul><div id="Content"><div class="Show">Content Area</div><div>Content1</div><div>Content2</div></div>
Then add a style:
*{ padding:0; margin: 0; }Body{ margin:0 ;} #nav{ width: + px; height: px; line-height: . list-style: none; Border-radius: px; margin-bottom: 5px; }#nav Li{ float: left; padding-right: tenpx; border-left: 1px solid green; font-size: 2em; font-family: "Microsoft Jas Black"; cursor: pointer; }#nav Li. current{ background: #abcdef;} #nav Li: Hover{ color: #fff; background: red; cursor: pointer; }#content Div{ width: px; Height: + px; Border:1px solid blue; box-sizing:border-box; display: none; }#content Div. Show{ display: block;}
Pure JavaScript Control performance:
Window.onload= function(){varUl=document.getelementbyid ("NAV");varLi=ul.getelementsbytagname ("Li");varContent=document.getelementbyid ("Content");varDiv=content.getelementsbytagname (' div '); for(varI=0; i<li.length;i++) {li[i].index=i; li[i].onclick= function(){ for(varj=0; j<li.length;j++) {li[j].classname=""; div[j].style.display="None"; } This. classname="Current"; div[ This. index].style.display="Block"; }}}
With JS write code more, and there are 2 layers of loop is very forgive. So I wrote it in jquery:
JQ Code:
$(function(){ $("ul li").click(function(){ $(this).addClass("current").siblings().removeClass("current"); $("#content div").eq($(this).index()).show().siblings().hide();})})
Basically 2 lines of code are done, because jquery supports powerful chained operations, so the above code can be reduced to one line:
$(this).addClass("current").siblings().removeClass("current").parent().next().children().eq($(this).index()).show().siblings().hide();
I have to say that jquery is really powerful, the study of its source code is defined as a powerful selector.
jquery's powerful selector vs. JavaScript.