我在页面上有一些图像,我想在每个图像下放置<span>
标记,使用'alt'文本,使用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>
我需要把它放在数组中吗?还是其他想法??
请帮帮我,谢谢!!!!!!
答案 0 :(得分:4)
尝试使用each
$(".container img").each(function() {
$(this).after("<span>"+$(this).attr('alt')+"</span>");
});
或更好一点(在我看来)
$(".container img").each(function() {
$(this).after($("<span/>").text($(this).attr('alt')));
});