2009-07-08 32 views
1

一個我網頁上的一些圖像,我希望把<span>標籤波紋管各的圖像,與「ALT」文字,用jQuery的jQuery,圖片,選擇

我開始:

HTML

<div class='container'> 
    <img src='images/1.jpg' alt='text1' /> 
    <img src='images/2.jpg' alt='text2' /> 
    <img src='images/3.jpg' alt='text3' /> 
</div> 

jQuery的

$(".container img").after("<span>"+$(".container img").attr('alt')+"</span>"); 

,但它把所有的<span>第一ALT

我需要獲得:

<div class='container'> 
     <img src='images/1.jpg' alt='text1' /> 
     <span>text1</span> 
     <img src='images/2.jpg' alt='text2' /> 
     <span>text2</span> 
     <img src='images/3.jpg' alt='text3' /> 
     <span>text3</span> 
    </div> 

我需要把它放在陣列?或者其他想法?

請幫助我,謝謝!!!!!!

回答

4

嘗試使用each

$(".container img").each(function() { 
    $(this).after("<span>"+$(this).attr('alt')+"</span>"); 
}); 

或者更好一點(在我看來)

$(".container img").each(function() { 
    $(this).after($("<span/>").text($(this).attr('alt'))); 
}); 
+0

是啊,這工程...礦井沒有....哎呀 – 2009-07-08 08:09:55