0

如何获取具有相同类的图像的 src 值并将它们写入/输出到空 div 中?

例如:

<body>
    <img class="class1" src="http://www.mysite.com/img/image1.jpg" width="168" height="168" alt="">
    <img class="class1" src="http://www.mysite.com/img/image2.jpg" width="168" height="168" alt="">
    <img class="class1" src="http://www.mysite.com/img/image3.jpg" width="168" height="168" alt="">
    <img class="class1" src="http://www.mysite.com/img/image4.jpg" width="168" height="168" alt="">
    <img class="class1" src="http://www.mysite.com/img/image5.jpg" width="168" height="168" alt="">
    <img class="class2" src="http://www.mysite.com/img/image6.jpg" width="168" height="168" alt="">
    <img class="class2" src="http://www.mysite.com/img/image7.jpg" width="168" height="168" alt="">
    <img class="class2" src="http://www.mysite.com/img/image8.jpg" width="168" height="168" alt="">
    <img class="class2" src="http://www.mysite.com/img/image9.jpg" width="168" height="168" alt="">
    <img class="class2" src="http://www.mysite.com/img/image10.jpg" width="168" height="168" alt="">

    <div id="print-src">
             <!-- Print Result Here -->
    </div>
</body>

结果(如果我只想检索具有 class1 类的图像的 src 值):

<body>
     ...........................

    <div id="print-src">
        http://www.mysite.com/img/image1.jpg
        http://www.mysite.com/img/image2.jpg
        http://www.mysite.com/img/image3.jpg
        http://www.mysite.com/img/image4.jpg
        http://www.mysite.com/img/image5.jpg
    </div>
</body>
4

1 回答 1

2

尝试这个:

var prn = $('#print-src');
$('img.class2').each(function () {
    prn.append('<p>' + this.src + '</p>');
});

演示在这里

于 2013-10-18T21:20:49.323 回答