代码之家  ›  专栏  ›  技术社区  ›  Hooray Im Helping codaddict

使用jQuery,如何查找出现在另一个类之间的类的所有实例

  •  1
  • Hooray Im Helping codaddict  · 技术社区  · 15 年前

    例如:

    <table>
      <tr class="highlight" id="click">
        <td>Rusty</td>
      </tr>
      <tr class="indent">
        <td>Dean</td>
      </tr>
      <tr class="indent">
        <td>Hank</td>
      </tr>
      <tr class="highlight">
        <td>Myra</td>
      </tr>
    </table>
    

    当我点击带有id的hr时说 click indent highlight

    2 回复  |  直到 15 年前
        1
  •  7
  •   Tatu Ulmanen    15 年前
    $('tr.highlight').click(function() {
        var $indents = $(this).nextAll('tr.indent');
    });
    

    编辑

    这似乎是选择全部。缩进不考虑。高光。试试这个:

    $('tr.highlight').click(function() {
        var row = $(this).next();
        var selection = false;
        while(row.hasClass('indent')) {
            if(!selection) {
                selection = row;
            } else {
                selection.add(row);
            }
            row = row.next();
        }
    });
    
        2
  •  0
  •   Wil    14 年前

    http://jsfiddle.net/w_barath/bFZnH/2/

    $("#click").bind('click', function() {
        var good = true;
        $("#click ~ tr").each(function(e) {
            if (this.className == "highlight") {
                good = false;
            }
            if (good && this.className == "indent") {
                this.style.background="red";
            }
        });
    });