jquery可以从一堆图像中获取src

jquery可以从一堆图像中获取src,jquery,src,attr,Jquery,Src,Attr,我有一些我无法控制的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> </d

我有一些我无法控制的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”分区都包含图像。我正在寻找循环并获取所有图像的src,将它们添加到一个数组中,然后将它们移动到列表标记中,这样它就可以与我正在使用的插件一起工作。我可以抓取1个img的源代码没问题,但我正在努力抓取所有8个src。关于实现这一点的最佳方法有什么建议吗?

您可以通过
$(“.img字段”)选择img src属性。查找(“img”).attr(“src”)


然后,您可以使用jquery
.each()
函数对它们进行循环。您可以通过
$(“.img字段”).find(“img”).attr(“src”)选择img src属性


然后您可以使用jquery
循环它们。each()
函数

您只需创建一个数组,并使用jquery
循环所有特定的
标记。each()
函数:


您可以简单地创建一个数组,并使用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'));
});
编辑:我刚刚看到了Rion的答案,它做了所有你需要它做的事情。

做这个-

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

编辑:我刚刚看到了Rion的答案,它完成了所有需要它做的事情。

类为.img字段的div没有src属性。它是child does.oops,.find(“img”)函数将对此有所帮助。具有.img字段类的div没有src属性。这是child does.oops,.find(“img”)函数将对此有所帮助。谢谢大家的提示/回复/建议。我可以得到一个src好的,这是每个部分,我没有任何乐趣。我会试试你的解决办法。再次感谢!谢谢大家的提示/回复/建议。我可以得到一个src好的,这是每个部分,我没有任何乐趣。我会试试你的解决办法。再次感谢!与我的答案相比,我更喜欢这个答案。我总是忽略贴图功能+1与我的答案相比,我更喜欢这个答案。我总是忽略贴图功能+1.
$('.img-field').each(function() {
    var thisSRC = $(this).find('img').attr('src'); // thisSRC is the current image source
    // do something with thisSRC
});