2013-05-21 30 views
0

所以,我想這一點:在jQuery中,你如何選擇當前表TR而不是任何嵌套表TR?

input.find(" tr").each(function(){ ... }); 

我也試過這樣:

input.find(" > tr").each(function(){ ... }); 

這兩個不工作。第一個將選擇任何TR,即使它在嵌套表下。如果桌子有任何東西或其他類似的東西,第二個就不行。任何幫助?

輸入被定義爲:

var input = $(".mySelectionScope"); 

的DOM應該是這樣的:

<div class='mySelectionScope'> 
     <div> // Optional 
      <table> 
       <tbody> 
        <tr> // Select this one 
         <table> 
          <tr> // Don't select this one 
          .... 

回答

0

這種解決方案也無所謂你是否有在之間的TBODY標籤:

$('table').find('tr:first').parent().children() 
0

可以過濾掉TR的下TR是一個使用.not()

input.find("tr").not("tr tr") 
0

你可以過濾它:

input.find("tr").filter(function(){return !$(this).closest('tr').length}).each(...); 
0

使用jQuery兒童()代替find()方法

從文檔:

的。孩子()方法.find()在不同的。孩子( )只有 沿着DOM樹行進一級,而.find()可以遍歷 向下多級選擇子孫元素(孫輩, 等)。

可選的DIV也產生了一些trickiness ...

var input = $('.mySelectionScope').children('div').length == 0 ? 
     $('.mySelectionScope') : 
     $('.mySelectionScope > div'), 
    rows = input.children('table').children('tbody').length == 0 ? 
     input.children('table').children('tr') : 
     input.children('table').children('tbody').children('tr'); 

如果TBODY在所有的瀏覽器總是插,if語句是沒有必要的,你可以改用

rows = input.children('table').children('tbody').children('tr'); 
第二

很醜,但你不能單獨使用選擇器。