使用jquery在最后一个图像后附加html

时间:2012-08-09 09:13:07

标签: javascript jquery

如何在最后一个img后附加以下html:

//create the icon
$('div.lookup-image-wrapper > div img').append('<div class="icon"></div>'); 

这是我的标记:

<div class="lookup-image-wrapper" style="text-align: center">

        <div class="divProductPic" id="divProductPic26039">
            <img alt="200 D" border="0" id="ProductPic26039" name=
            "ProductPic26039" onclick="popuplarge_26039()" src=
            "images/Product/medium/26039.jpg" style=
            "cursor:hand;cursor:pointer;" title=
            "Click here to view larger image">
        </div>


    <img alt="Show Picture 1" border="0" class="image-nav" onclick="_(1);" src="images/PRODUCT/icon/26039_1_.jpg"
        style="cursor:hand;cursor:pointer;">
    <img alt="Show Picture 2" border="0" class="image-nav" onclick="_(2);" src="images/PRODUCT/icon/26039_2_.jpg"
         style="cursor:hand;cursor:pointer;"><img alt="Show Picture 3" border="0" class="image-nav" onclick="setcolorpicidx_26039(3);" src=
        "images/PRODUCT/icon/26039_3_.jpg" style="cursor:hand;cursor:pointer;">

            <!--[#### THE DIV ICON NEEDS TO GO HERE #####]-->

     <div id="prodEmbiggen">
        <a href="javascript:void(0);" onclick=""><img align="absmiddle" alt="" border="0" src="showlarger.gif" title=
          "Click for larger image"></a>
     </div>
</div>

2 个答案:

答案 0 :(得分:3)

试试这个:

$('div.lookup-image-wrapper > img:last').after('<div class="icon"></div>'); 

答案 1 :(得分:2)

要在最后一张图像后附加内容,请使用最后一个选择器(:last)

$('div.lookup-image-wrapper > div img:last').append("your content");

如果您要添加大量内容,可以在此处缩小HTML http://www.willpeavy.com/minifier/