【问题标题】:traversing dom in jquery find previous instance of class在jquery中遍历dom找到类的前一个实例
【发布时间】:2009-08-19 09:01:08
【问题描述】:

我有以下表格和按钮的重复模式

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

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

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

现在,当我单击按钮 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();
    });
});

【问题讨论】:

    标签: javascript jquery dom


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      好的,我设法修复它:

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

      【讨论】:

        【解决方案3】:

        假设:按钮集合中的按钮索引与表格行集合中表格行的索引匹配。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-01-20
          • 2010-12-07
          • 1970-01-01
          • 2014-06-30
          • 2010-09-21
          相关资源
          最近更新 更多