2011-11-13 28 views
1

我想更改包含給定鏈接的「tr」元素的背景顏色。如何更改包含特定鏈接的<tr>的背景顏色?

我摘錄:

var color1 = "red"; 
var targetForum = $("tr a[href*='showforum=28']"); 

targetForum.each (function() { 
    var thisRow = $(this).parent().parent().parent();   
    thisRow.style.backgroundColor = color1; 
} 

我試過RGB值以及十六進制,但什麼也沒有發生的背景顏色。

目標頁面如下所示:

<tr> 
    <td class="row2" align="center"> 
    <td class="row1" width="3%" align="center"> 
    <td class="row1"> 
    <td class="row1" width="15%"> 
     <span class="forumdesc"> 
      <a title="Off-Topic" href="showforum=41">ForumABC</a> 
     </span> 
    </td> 
    <td class="row1" align="center"> 
    <td class="row2" align="center"> 
    <td class="row1" align="center">223460</td> 
    <td class="row1"> 
</tr> 
+0

你已經安裝了螢火蟲?檢測這種錯誤非常有用。 – hugomg

回答

1
  1. 看起來你在這裏使用jQuery,它是如何加載(通過論壇或GM)
  2. 當jQuery是通過加載必須使用unsafeWindow.$
  3. 父()訪問的jQuery論壇返回jQuery的對象,則不能設置使用obj.style一個jQuery對象的樣式屬性,使用css()代替

我建議這樣的:(你不需要每次)

unsafeWindow.$("tr:not(:has(tr)):has(a[href*='showforum=28'])").css('backgroundColor','red'); 
+0

我知道我錯過**的東西**,但*爲什麼*'unsafewWindow。$'?現在,我開始後悔,最後一杯酒... =/ –

+0

閱讀此:** http://wiki.greasespot.net/UnsafeWindow**。如果您想訪問窗口/文檔中的用戶定義對象(例如jQuery),則需要使用unsafeWindow指向窗口對象。原生JS/DOM對象不受此限制的影響 –

+0

但是當jquery由GM加載時(** http://wiki.greasespot.net/Third-Party_Libraries**),您不需要使用unsafeWindow –

0

設置樣式jQuery的方式(如指出,thisRow是一個jQuery對象)。
那將是:thisRow.css ('backgroundColor', color1);

但是,如果網站使用背景圖片,顏色可能不會生效。使用:

thisRow.css ('background', color1); 

確保任何BG圖像不掩蓋顏色變化。

最後,該網站可能會對錶單元格的BG進行樣式設置,這可能會再次掩蓋行顏色更改。爲了解決這個問題,請使用<td>元素。

因此,這將是:

targetForum.each (function() { 
    var thisRow = $(this).parent().parent().parent();   
    thisRow.css ('background', color1); 
    thisRow.find ("td").css ('background', color1); 
}