1

例えば:

<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 をクリックすると、 classの次のインスタンスまで、clickclass のすべてのインスタンスをどのように見つけることができるでしょうか?indenthighlight

4

2 に答える 2

7
$('tr.highlight').click(function() {
    var $indents = $(this).nextAll('tr.indent');
});

編集

.highlights に関係なく、すべての .indents を選択しているようです。これを試して:

$('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();
    }
});
于 2009-12-08T17:08:27.790 に答える
0

これは、クリックを「クリック」でハイライトにのみ適用し、「クリック」と次のハイライトの間のインデントのみを返します。

jQuery ~ "next siblings" セレクターを使用して、もう少し作業を行います。

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";
        }
    });
});
于 2011-03-28T01:52:36.610 に答える