简体   繁体   English

单击一个列表项应显示与该列表项相关的表

[英]Clicking a list item should display table related to that list item

When the user selects the list item, the table associated with that list item should be displayed. 当用户选择列表项时,应显示与该列表项关联的表。 I am having a brain block, I think I'm close to solving it or at least the idea on how to. 我有一个大脑障碍,我想我即将解决它,或者至少是关于如何解决的想法。 Find the length of each, if the length equals one another, then create the show/click functionality. 找到每个的长度,如果长度相等,则创建显示/单击功能。 The answer should be one that can be used for hundreds of list items with different tables. 答案应该是可以用于具有不同表的数百个列表项的答案。

HTML HTML

<div>
            <ul>
                <li id='one'><a href="#">number</a></li>
                <li id='two'><a href="#">othernumber</a></li>
            </ul>
        </div>
        <div>
            <table>
                <thead>
                    <tr>
                        <th>title</th><th>title</th><th>title</th><th>title</th>
                    </tr>
                </thead>
                <tbody id="table1">
                    <tr>
                        <td>blah</td>
                        <td>blah</td>
                        <td>blah</td>
                        <td>blah</td>
                    </tr>

                </tbody>
                <tbody id="table2" style="display:none">
                    <tr>
                        <td>blahTwo</td>
                        <td>blahTwo</td>
                        <td>blahTwo</td>
                        <td>blahTwo</td>
                    </tr>
                </tbody>
            </table>
        </div>

JS JS

function count(){
    var list = jQuery('ul li')

    var table = jQuery('table tbody')

    if(table.length == list.length){
        jQuery(list).click(function(){
            //show the table length that is equal to the list length 

        })
    }
}

jsfiddle 的jsfiddle

If you want to associate the list items to the tbody elements just based on their position in the list, you can make use of the .index() method in conjunction with .eq() : 如果要仅基于列表项中的位置将列表项与tbody元素相关联,则可以将.index()方法.eq()结合使用:

 var list = jQuery('ul li a') var table = jQuery('table tbody').hide() list.click(function(e) { table.hide().eq(list.index(this)).show() }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <ul> <li id='one'><a href="#">number</a></li> <li id='two'><a href="#">othernumber</a></li> </ul> </div> <div> <table> <thead> <tr> <th>title</th> <th>title</th> <th>title</th> <th>title</th> </tr> </thead> <tbody id="table1"> <tr> <td>blah</td> <td>blah</td> <td>blah</td> <td>blah</td> </tr> </tbody> <tbody id="table2" style="display:none"> <tr> <td>blahTwo</td> <td>blahTwo</td> <td>blahTwo</td> <td>blahTwo</td> </tr> </tbody> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM