jquery,图像,选择器

时间:2009-07-08 08:01:16

标签: jquery html image

我在页面上有一些图像,我想在每个图像下放置<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>

我需要把它放在数组中吗?还是其他想法??

请帮帮我,谢谢!!!!!!

1 个答案:

答案 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')));
});