2011-04-25 83 views
1

我有一個頭3標記內的圖像,我有一個jQuery的功能,展開和摺疊表。當功能完成時,我需要更改展開/摺疊圖標,我可以這樣做。我的問題是,即使它沒有ID,我是否可以更改h3標籤內的圖像?有沒有辦法在jQuery中沒有id的img標籤上設置背景圖片?

因此,像:

$(this.img).css("backgroundImage", "url(images/expand.png)"); 

回答

1
$('h3').find('img').attr('src', 'url(images/expand.png)') 

或者因爲你有切換中的引用:

$(this).find('img').attr('src', 'url(images/expand.png)') 
0

你爲什麼要設置CSS,而不是改變圖像的src?試試 $("img", this).attr("src", "images/expand.png");

+0

對不起,我想通了這一點,我張貼此之後,但我仍想知道的無標識的一部分。 – Dennis 2011-04-25 20:13:42

0

是的,這是可能的。你有一個錯字。 backgroundImage應該background-image這樣的:

$(this.img).css("background-image", "url(images/expand.png)"); 
0

當然..

$('h3 img').css(/* --- */); 

只要不使用.img,因爲這是所謂的 「IMG」 CSS類。

0

當然,

$(this).animate({'height':'toggle'},function(){$(this).find('img').attr('src','pathtoimage.png');}) 
相關問題