jquery's powerful selector vs. JavaScript.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.