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 に答える