0

コントロールできない HTML があります。基本的なレイアウトは次のとおりです...

<div class="container">
    <div class="img-field">
        <img src="/path/to/img.jpg" />
    </div>
    <div class="img-field">
        <img src="/path/to/img.jpg" />
    </div>
</div>

画像を含む約 8 つの「.img-field」div があります。ループしてすべての画像の src を取得し、それらを配列に追加してから、それらをリストタグに移動して、使用しているプラ​​グインで動作できるようにしようとしています。1 つの img のソースを問題なく取得できますが、8 つの src をすべて取得するのに苦労しています。これを達成するための最善の方法について何かアドバイスはありますか?

4

4 に答える 4

3
var arr = $('.img-field img').map(function(){
               return this.src;
          }).get();
于 2013-01-02T22:07:27.177 に答える
2

<img>jQuery.each()関数を使用して、配列を作成し、特定のタグをすべてループするだけです。

//Your array to store your values
var yourImageSources = new Array();

//Iterates through each of the image fields
$('.img-field').each(function()
{
    //Selects the img element within the current image field and pushes it onto
    //the array.
    yourImagesSources.push($('img',this).attr('src'));
});

于 2013-01-02T21:55:53.873 に答える
0

これを行う -

$('.img-field').each(function() {
    var thisSRC = $(this).find('img').attr('src'); // thisSRC is the current image source
    // do something with thisSRC
});

編集:そして、必要なことをすべて行うリオンの答えを見ました。

于 2013-01-02T21:57:37.147 に答える
0

You can select the img src attribute by $(".img-field").find("img").attr("src")

and then you can loop over them using the jquery .each() function

于 2013-01-02T21:55:09.537 に答える