javascript 使用jQuery获取div中每个图像的来源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14709797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:22:46  来源:igfitidea点击:

Get source of each image in div with jQuery

javascriptjqueryhtml

提问by streetlight

I'm trying to do a seemingly simple loop, but I keep running into issues.

我正在尝试做一个看似简单的循环,但我一直遇到问题。

I'm trying to loop through a specific div (targeted by an ID), and return the source of the two images inside of it. Here is my HTML:

我正在尝试遍历特定的 div(以 ID 为目标),并返回其中两个图像的源。这是我的 HTML:

<div id="container">
   <div class="row selected" id="one"><img src="onei.png"><img src="twoi.png"></div>
   <div class="row" id="two"><img src="onei1.png"><img src="twoi2.png"></div>
 </div>

And here is my loop:

这是我的循环:

function loop() {
    alert()
    $('#container row.selected img').each(function() {
        alert($(this).attr('src'))
    });
}

I can't seem to find out why this isn't working. Shouldn't this loop through each image in my targeted div and alert the source?

我似乎无法找出为什么这不起作用。这不应该循环遍历我的目标 div 中的每个图像并提醒源吗?

回答by John Boker

maybe something like

也许像

function loop() {
    alert()
    $('#container .row.selected img').each(function() {
        alert($(this).attr('src'))
    });
}

you forgot the .on row

你忘了.row

回答by Ilyssis

Maybe you missed the point:

也许你错过了重点:

'#container .row.selected img'