2013-07-08 35 views

回答

1

細胞是不是在HTML居然連續的,所以在相同的水平,你不能只使用一切(與nextAll等)。假設你有這個表:

<table> 
    <tbody> 
     <tr> 
      <td>one</td> 
      <td id="two">two</td> 
      <td>three</td> 
     </tr> 
     <tr> 
      <td>four</td> 
      <td>five</td> 
      <td>six</td> 
     </tr> 
    </tbody> 
</table> 

...你要突出「之後」 two的每一個細胞,這將遍歷並選擇那些細胞:

$('table td:gt(' + $('#two').index() + ')').each(function() { 
    console.log($(this).text()); // or whatever you want to do with the cell 
}); 

演示Fiddle here

-1
// Selecting all <td>s after <td id="i-want"> 
$('td#i-want ~ td') 

// Selecting the next <td> after <td id="i-want"> 
$('td#i-want + td') 
+0

不'〜'只選擇具有相同父元素的元素 - 在這種情況下,''? – jterry

0

可能不是最有效的解決方案,但它能夠完成任務:

這裏的基本策略是:(a)選擇兄妹​​當前單元格後,(B)跳到行元素,爲每在當前行的後面,抓取TD元素並添加到同級列表中。

<!doctype html> 
<html> 
<head><script src="../lib/jquery-1.9.1.js"></script> 
<style> 
table { border:1px solid black; } 
thead th { border-bottom:1px solid black; } 
tfoot th { border-top:1px solid black; } 
</style> 
<script> 
$(function() { 

    $('tbody').find('td') 
     .click(function(event) { 
      var $cell   = $(event.target), 
       $siblings  = $cell.nextAll('td'), 
       $remainingCells = $cell.closest('tr').nextAll().find('td'); 

      $siblings.add($remainingCells).each(function(index, element) { 
       console.info(index + ':', $(element).text()); 
      }); 
     }); 

}); 
</script> 
</head> 
<body> 
<table> 
    <thead> 
     <tr> 
      <th>#</th> 
      <th>A</th> 
      <th>B</th> 
      <th>C</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>1</td> 
      <td>Alpha</td> 
      <td>Bravo</td> 
      <td>Charlie</td> 
     </tr> 
     <tr> 
      <td>2</td> 
      <td>Delta</td> 
      <td>Echo</td> 
      <td>Foxtrot</td> 
     </tr> 
     <tr> 
      <td>3</td> 
      <td>Golf</td> 
      <td>Hotel</td> 
      <td>India</td> 
     </tr> 
    </tbody> 
    <tfoot> 
     <tr> 
      <th>#</th> 
      <th>A</th> 
      <th>B</th> 
      <th>C</th> 
     </tr> 
    </tfoot> 
</table>