Jquery matches display and hide of control layer by element index

Source: Internet
Author: User

This article will introduce in detail the display and hiding of the element-based index matching control layer of jquery. You can use hide (), show (), then we can use each traversal to control display hiding.

The structure of a topic is special during work. The Code is as follows:

The Code is as follows: Copy code

<Div class = "test">
<Ul class = "list">
<Li> 1 </li>
<Li> 2 </li>
<Li> 3 </li>
<Li> 4 </li>
<Li> 5 </li>
</Ul>
<Div class = "part">
<Div> 1-1 </div>
<Div style = "display: none;"> 2 </div>
<Div style = "display: none;"> 3 </div>
<Div style = "display: none;"> 4 </div>
<Div style = "display: none;"> 5 </div>
</Div>
</Div>
<Div class = "test">
<Ul class = "list">
<Li> 1 </li>
<Li> 2 </li>
<Li> 3 </li>
<Li> 4 </li>
<Li> 5 </li>
</Ul>
<Div class = "part">
<Div> 2-1 </div>
<Div style = "display: none;"> 2-2 </div>
<Div style = "display: none;"> 2-3 </div>
<Div style = "display: none;"> 2-4 </div>
<Div style = "display: none;"> 2-5 </div>
</Div>
</Div>

You need to click the li label under each. list to control the display of the div layer of. part at the same level under this. list. At the same time, it cannot affect the display of other. list-related layers on the page!

At the beginning, I felt that there was no technical difficulty in dealing with this, but I encountered a small problem and recorded the implementation code.

The Code is as follows: Copy code


<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta http-equiv = "X-UA-Compatible" content = "IE = edge, chrome = 1">
<Title> Examples </title>
<Meta name = "description" content = "">
<Meta name = "keywords" content = "">
<Link href = "" rel = "stylesheet">
<Style>
* {Margin: 0; padding: 0 ;}
. Test {padding: 20px ;}
. Test ul {overflow: hidden ;}
. Test ul li {width: 50px; height: 30px; margin-right: 10px; border-bottom: 1px red solid; float: left; cursor: pointer ;}
</Style>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"> </script>
</Head>
<Body>
<Div class = "test">
<Ul class = "list">
<Li> 1 </li>
<Li> 2 </li>
<Li> 3 </li>
<Li> 4 </li>
<Li> 5 </li>
</Ul>
<Div class = "part">
<Div> 1-1 </div>
<Div style = "display: none;"> 2 </div>
<Div style = "display: none;"> 3 </div>
<Div style = "display: none;"> 4 </div>
<Div style = "display: none;"> 5 </div>
</Div>
</Div>
<Div class = "test">
<Ul class = "list">
<Li> 1 </li>
<Li> 2 </li>
<Li> 3 </li>
<Li> 4 </li>
<Li> 5 </li>
</Ul>
<Div class = "part">
<Div> 2-1 </div>
<Div style = "display: none;"> 2-2 </div>
<Div style = "display: none;"> 2-3 </div>
<Div style = "display: none;"> 2-4 </div>
<Div style = "display: none;"> 2-5 </div>
</Div>
</Div>
<Script>
Var index,
Parent;
$ (". List> li "). each (function (I, el) {/* multiple. the li indexes under the list are accumulated, that is, the I value is: 0 <= I <= 9 */
$ (This). click (function (event ){
Parent = $ (this). parents (". test ");
Index = $ (". list> li ", parent ). index ($ (this); // obtain the corresponding parent element of the click item. test. the index in the part instead of the I value in the each ().
// Console. log (index );
Parent. children ('. part'). find ('div'). eq (index). show (). siblings (). hide ();
});
});
</Script>
</Body>
</Html>

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.