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

时间:2022-12-31 20:37:15

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中的每个图像并提醒源吗?

2 个解决方案

#1


12  

maybe something like

也许是这样的

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

you forgot the . on row

你忘记了。在行上

#2


2  

Maybe you missed the point:

也许你错过了这一点:

'#container .row.selected img'

#1


12  

maybe something like

也许是这样的

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

you forgot the . on row

你忘记了。在行上

#2


2  

Maybe you missed the point:

也许你错过了这一点:

'#container .row.selected img'