我有一段jQuery,如下所示,它将我的图像包装在一个href标签中,这样它们就可以在一个灯箱中使用。
我还需要调整alt标记中出现的内容,它需要从用户在ckeditor中的标题框中所放的任何内容中生成(在每个图像下生成一个图形标题标记)。我需要能够将该数据插入空的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>');
});
谢谢你的帮助
发布于 2015-07-13 12:44:12
如果我正确理解,您希望更改图像的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>');
});
https://stackoverflow.com/questions/31382314
复制相似问题