2012-02-10 21 views
0

我在頁面上有很多表,但與jQuery我只需要得到table其中有與類ms-vh2-nograd,所有其他我通過。與jQuery檢索表和更改所有th的顏色

因此,換句話說,我搜索th.ms-vh2-nograd,我需要得到父父,我認爲 ,然後在table我需要的背景顏色設置爲白色每個th

這是我有的代碼,但它不工作,任何人有任何想法?

jQuery(document).ready(function() 
{ 
    $table=$("table.ms-listview ms-vh2-nograd") 
    $table.("th").each(function() 
    { 
     this.style.backgroundcolor="white" 
    }); 
}); 

UPDATE: 在那些日的環節不能有懸停......功能

+1

什麼是表結構的HTML結構提供的HTML代碼 – 2012-02-10 14:41:18

+0

*「更新:和那些th的鏈接不能懸停在...功能」*。我們該如何知道這一點?如何添加懸停功能。您正在爲您的問題添加更多要求,但沒有更多信息。 – 2012-02-10 14:58:42

回答

1
this.style.backgroundcolor="white" 

應該是:

$(this).css('background-color','white') 
+0

這只是問題的一小部分。 – 2012-02-10 14:56:12

1

嘗試

$table.find("th").each(function() 
{ 
    ... 

代替。

+0

除非我錯了,'.children()'只能在DOM的一個層次上出現。這可能取決於表格的HTML結構,但如果他們在'

'內使用'',''或''標籤,那麼它將不會找到所選元素。 – 2012-02-10 14:52:38

+0

很對;固定。 – izb 2012-02-10 15:23:03

-1

我猜 「MS-VH2-諾格拉德」 是一類,那麼試試這個:

jQuery(document).ready(function() 
{ 
    $table=$("table.ms-listview .ms-vh2-nograd") 
    $table.("th").each(function() 
    { 
     this.style.backgroundcolor="white" 
    }); 
}); 
0

嘗試這樣的:

jQuery(document).ready(function(){ 
    $(".ms-vh2-nograd th").each(function(){ 
     $(this).css('backgroundcolor','white'); 
    }); 
}); 
1

*「我對頁很多我只需要得到table其中有1 th同類ms-vh2-nograd

...

「我搜索th.ms-vh2-nograd,我需要得到家長父母我想,然後在table每個th我需要的背景顏色設置爲白色」 *

當你說table其中有1 th與等級ms-vh2-nograd,我假設你的意思是至少有一個,或者永遠不會超過一個。

如果th元素都是相同的行中,你可以這樣做

$("table.ms-listview th.ms-vh2-nograd").siblings() 
             .andSelf() 
             .css('backgroundColor',"white"); 

這得到了th.ms-vh2-nograd,然後抓住它的兄弟姐妹,最後增加了原選區重新使用andSelf()。然後使用.css()來設置樣式。


如果還有其他行中th元素,然後使用.closeset('table').find('th'),讓他們...

$("table.ms-listview th.ms-vh2-nograd").closest('table') 
             .find('th') 
             .css('backgroundColor',"white"); 
+0

真的很好,但如果這個圖像有圖像...我將背景設置爲白色,它會覆蓋圖像還是...我怎麼說沒有圖像? – Alnedru 2012-02-10 15:01:40

+0

@ user1002583:而不是'backgroundColor'只是使用'background'。 – 2012-02-10 15:10:02

0

好吧,這是你的代碼:

jQuery(document).ready(function() 
{ 
    $table=$("table.ms-listview ms-vh2-nograd") 
    $table.("th").each(function() 
    { 
     this.style.backgroundcolor="white" 
    }); 
}); 

有許多與此問題。首先,在聲明變量時不使用關鍵字var,因此您(可能)使用不需要存在的變量來污染全局範圍。

的選擇table.ms-listview ms-vh2-nograd試圖檢測與標籤<ms-vh2-nograd>創建(這顯然不存在)的元素,因爲它是你需要使用`table.ms-listview .ms-vh2-nograd類 - 如果你只關心<th>元素有這個類,您可能需要使用table.ms-listview th.ms-vh2-nograd。然而,該選擇器標識了一組<th>元素 - 而不是包含它們的表格(因此變量名稱具有誤導性)。

之後的行中存在語法錯誤 - $table.("th")無效 - 您需要在$table上調用jQuery函數。

最後,不需要使用.each()來設置所有匹配元素的CSS屬性 - 調用集合上的.css()將應用它到包含在其中的所有元素。

我修改代碼這樣:

$(function() { 
    var $table = $('table.ms-listview .ms-vh2-nograd').closest('table'); 
    $($table).find('th').css('background-color','white'); 
}); 

即標識包含<th>元件(不是<th>元件本身),並將其存儲在$table變量(其可以被限制在功能表中,因爲它不需要是全球性的)。然後它找到該<table>元素中的所有<th>元素,並將CSS屬性應用於所有元素。