2013-11-22 73 views

回答

1

請享用 ..!!

$(document).ready(function() { 
    $('.photogalleryItem').append(function() { 
     return '<div class="caption">' + $(this).find('img').attr('alt') + '</div>'; 
    }); 
}); 


參考

.append()

+1

非常感謝Tushar :) –

+0

@wanghaipeng歡迎高興幫忙:) –

0

嘗試:

$(document).ready(function(){ 
    $("td.photogalleryItem").each(function(){ 
     $(this).find(".caption").text($(this).find("img").attr("alt")); 
    }); 
}); 
+0

謝謝Hiral,我試過了,但它不工作。 :( –

+0

@wanghaipeng確保您有jQuery的文件,並在'document.ready'寫。看到我的編輯答案。 – Hiral

+0

仍然沒有工作:( –

0

這裏是我的解決方案

$(function(){ 
    $('td.photogalleryItem').each(function(e) { 
     var $caption = $('<div class="caption">'); 
     $caption.text($('img', $(this)).attr('alt')) 
     $(this).append($caption); 
    } 
}); 

這將遍歷每個td.photogalleryItem並添加標題用正確的alt文本。你的初始代碼是一個開始,但問題是它只得到第一個td.photogalleryItemalt文本。