简体   繁体   English

在jQuery中遍历dom查找类的先前实例

[英]traversing dom in jquery find previous instance of class

I have the following repeating pattern of tables and buttons 我有以下重复的表格和按钮模式

----------------------
table 1 .inventory class
----------------------
[button 1 .add-row]

----------------------
table 2 .inventory class
----------------------
[button 2 .add-row]

----------------------
table 3 .inventory class
----------------------
[button 3 .add-row]

now when i click button 1,2,3 i want to traverse to table 1,2,3 respectively (the previous table) and display an extra row o nthe table(already created, just hidden), I tried this but all the buttons only effect the first table first and once all those rows are displayed it then starts displaying on the next table etc etc, i know the answer is probably obvious.. heres my code: 现在,当我单击按钮1,2,3时,我想分别遍历表1,2,3(上一个表)并在表中显示额外的一行(已创建,刚刚隐藏),我尝试了此操作,但是所有按钮仅首先影响第一个表,并且一旦显示了所有这些行,然后就开始在下一个表等上显示,等等,我知道答案可能是显而易见的。

$(document).ready(function(){
    $('.additional-item-row').hide();
    $('.add-item').click(function(){
        $(this).prevAll(".inventory .additional-item-row:hidden:first").show();
    });
});

Try this: 尝试这个:

$(document).ready(function(){
    $('.additional-item-row').hide();
    $('.add-item').click(function(){
        $(this).prev("table").children(".inventory .additional-item-row:hidden:first").show();
    });
});

好吧,我设法解决它:

$(this).prevAll(".inventory:first").find(".additional-item-row:hidden:first").show();

Assumption: the index of the button in the set of buttons matches the index of the table row in the set of table rows. 假设:按钮在按钮组中的索引与表行集合中表行的索引匹配。

$(function() {
    $("button.add-item").click(function() {
        var index = $("button.add-item").index(this);
        $("tr.additional-item-row:eq(" + index + ")").show();
    });
});

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

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