2009-10-29 41 views
0

我有以下的HTML標記:如何使用jQuery更改A元素的背景?

<table class="rwTitlebarControls" cellspacing="0" cellpadding="0" align="left"> 
    <tbody> 
     <tr> 
      <td style="width: 16px;"> 
       <a class="rwIcon" style="background: transparent url(Images/ic_icon_16.png) no-repeat scroll 0px 0px; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"/> 
      </td> 
      <td> 
       <em unselectable="on">Title</em> 
      </td> 
      <td nowrap="" style="white-space: nowrap;"> 
      </td> 
     </tr> 
    </tbody> 
</table> 

我想改變<一個> - 元素的圖像的URL與使用jQuery類rwIcon,但我似乎無法找到.rwIcon 。

我可以用查找表含無問題:

$('table.rwTitlebarControls'); 

,但以下兩個選擇不返回元素,我需要

$('a.rwIcon') 

$('.rwIcon') 

我在做什麼錯?

NOTE:該HTML由Telerik的RadComponents自動生成,不是我可以更改或控制的東西,所以請嘗試回答問題並避免評論標記的質量。謝謝!

+0

您確定沒有獲得A標記,還是僅僅不改變背景?嘗試alert($('。rwIcon')。length); –

+1

你可以使用那樣的錨嗎?錨點應該有結束標記和href屬性imho ... – eugeneK

+0

1.您使用表格進行標記:錯誤。 2.你使用內聯css:錯誤。 – Natrium

回答

2

這並不工作:

$('.rwIcon') 

但一個與該類標籤是空的(也就是沒有內容)

這工作:

<a class="rwIcon" style="background: transparent url(Images/ic_icon_16.png) no-repeat scroll 0px 0px; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">SOME CONTENT</a> 

有了這個腳本:

$(".rwIcon").css({backgroundColor:"#000000"}); 
+0

這使我走上了正確的軌道。謝謝! – Cros

0

轉在我看來正確的元素,我只是沒有確定它是如此。以下腳本更改了a-tag的背景圖片:

function ChangeWindowIcon(icon) { 
    icon = 'url(Images/ic_' + icon + '_16.png)'; 
    $('a.rwIcon').css({ 'background-image': icon }); 
} 
相關問題