2013-03-13 49 views
2

我正在使用網格控件,其中表中有一個tr是隱藏的,點擊打開..所以我嘗試了下面的函數來將類添加到奇數行。添加類到tr替代

<script type="text/javascript"> 
// Activate TableStyling 
jQuery(document).ready(function() { 
    $('table.management').each(function() { 
     $(this).children('tbody').children(':odd').addClass('grey'); 
    }); 

}); 

但問題是,計算出隱藏的TR太..所以我需要不計隱藏的TR功能&或者添加類到觀看Tr的的

回答

2

嘗試

$(this).children('tbody').children(':visible:odd').addClass('grey'); 

http://jsfiddle.net/K3vCD/

+0

它的作品..謝謝 – MAR 2013-03-13 14:34:26

+0

PLZ發表評論投票後! – dakait 2015-11-30 09:31:24

1

嘗試增加:visible選擇。

$(this).children('tbody').children(':visible:odd').addClass('grey'); 
+0

謝謝親愛的@Upvoter。 – 2013-03-13 14:33:39

5

你可以這樣做:

$(this).children('tbody').children(':visible:odd').addClass('grey'); 

此外,如果你的隱藏tr有一些特殊的階級或東西,你可以壽這樣的:

$(this).children('tbody').children(':not(.<your hidden class>):odd') 
    .addClass('grey'); 
+0

它的作品..謝謝 – MAR 2013-03-13 14:35:55

1

大多數(如果不是所有的答案)都假設您的「隱藏」定義與jQuerys相同。在其他情況下,「隱藏」可能是像height:0px,含有重疊元素的模糊z-indexing等。在這些非常罕見的情況下,過濾器方法可以與您自己的邏輯一起使用來分類被認爲「隱藏」的內容。

$(this).children('tbody').filter(function(){ 
    return this.isNotAHiddenTR(); // your custom logic. 
    // return $(this).is(':visible'); // the most likely solution 
}).children(':odd').addClass('grey'); 

此外,有visibility: hidden:visible選擇返回元件。