jquery matches the display and hide of the control layer by element index

Source: Internet
Author: User

Work in contact with a topic of the structure is more special, 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>

Ask to click on the Li tag under each. List to control the display of the div layer of the. Part below the. Part, without affecting the display of the other. List-related layers on the page!

Just beginning to feel that there is no technical difficulty in dealing with this, but the hands of the time or encountered a little problem, record the implementation code

The code is as follows Copy Code


<! DOCTYPE html>
<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>
<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) {/*) The LI index under the list will accumulate, that is, I value: 0<=i<=9 * *
$ (this). Click (Function (event) {
parent=$ (This). Parents (". Test");
index=$ (". List >li", parent). Index ($ (this))//Get the click item in the corresponding parent element. Test. Part, not the I value in each ()
Console.log (index);
Parent.children ('. Part '). Find ("div"). EQ (index)-show (). siblings (). Hide ();
});
});
</script>
</body>

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.