2015-07-13 90 views
3

我有一塊jQuery,請參閱下面,將我的圖像包裝在href標記中,以便它們可以在Lightbox中使用。從<figcaption>覆蓋/插入新alt標記標記

我需要調整alt標籤中顯示的內容,並且它需要從用戶放入ckeditor中的標題框中生成(在每個圖像下生成一個figcaption標籤)。我需要能夠將這些數據插入到空alt =「」中,並且能夠覆蓋alt標籤中是否有任何當前內容。

正被目前的HTML輸出是:

<figure class="image"> 
    <a data-imagelightbox="f" href="/ckfinder/userfiles/files/Screenshot_South-Downs-Lines-RSC_51_23330-1_22091_10-00-48.jpg"> 
     <img width="1600" height="900" src="/ckfinder/userfiles/files/Screenshot_South-Downs-Lines-RSC_51_23330-1_22091_10-00-48.jpg" alt="Caption 1"></img> 
    </a> 
    <figcaption> 
     Caption 
    </figcaption> 
</figure> 

當前腳本我有如下:

$('#article-copy img').each(function() { 
    var $img = $(this), 
     alt = $img.next('figcaption').text(), 
     href = $img.attr('src'); 
    $img.wrap('<a href="' + href + '" data-imagelightbox="f"></a>'); 
}); 

感謝您的幫助

+0

這是HTML之前或之後的函數運行? – LinkinTED

+0

html是jquery運行後生成的內容,它目前只包含href中的圖像,但不會改變當前的alt標記,這正是我試圖實現的目標。謝謝 – Rob88991

回答

1

如果我沒有理解正確地,您想要更改圖像的alt屬性。然後這應該工作:

$('#article-copy img').each(function() { 
    var $img = $(this), 
     alt = $img.next('figcaption').text(), 
     href = $img.attr('src'); 
    $img.attr('alt', alt); 
    $img.wrap('<a href="' + href + '" data-imagelightbox="f"></a>'); 
}); 
+0

非常感謝,工作很棒:) – Rob88991

+0

不客氣! – LinkinTED