Javascript jQuery迭代表元素

Javascript jQuery迭代表元素,javascript,jquery,html,iteration,Javascript,Jquery,Html,Iteration,我试图使用jQuery从html网页中的img元素检索数据 我从一开始就知道只有一个映像,但是当我运行下面的代码时,我得到了两个警报框。 它们包含相同的信息 有人知道我做错了什么吗 $("#tableX td").find("img").each(function() { if ($(this).data("apple") == "orange") { alert($(this).attr("src")); } }); 谢谢 更新

我试图使用jQuery从html网页中的img元素检索数据

我从一开始就知道只有一个映像,但是当我运行下面的代码时,我得到了两个警报框。 它们包含相同的信息

有人知道我做错了什么吗

$("#tableX td").find("img").each(function() {
    if ($(this).data("apple") == "orange") {
        alert($(this).attr("src"));                
    }
});
谢谢

更新:

DOM输出

<td id="tdP4" align="center" style="border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(201, 201, 201); border-right-color: rgb(201, 201, 201); border-bottom-color: rgb(201, 201, 201); border-left-color: rgb(201, 201, 201); "><img id="imgP4" src="/images/t/00.jpg" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; width: 63px; height: 103px; display: block; background-color: rgb(71, 7, 79); " alt="00"></td>

也许可以尝试更具体的选择器

$("#tableX td > img").each(function() {
    if ($(this).data("apple") == "orange") {
        alert($(this).attr("src"));                
    }
});
编辑:您的
标签保持打开状态

只有一个表,每个tr一个td,和一个图像

这允许多个
tr
元素,每个元素内部都有
td
img


$(“#tableX td”).find(“img”)。每个
将查找
#tableX
中的所有
img
元素。它们是否在不同的行中并不重要(
tr
)。

可能有两个原因:

  • 或者有多行包含图像(选择器选择整个表中的所有图像)
  • 或者代码运行两次
表中似乎有两个以上的图像;)代码很好。你有实际页面的链接吗?你能显示html吗?另外,如果两个警报显示相同的
src
,则会很困难。。它非常依赖javascript,所有的dom操作都在那里完成。查看消息来源没有帮助。这只是一个包含一些tr和td以及每个td一个图像的基本表格。好的,显示警报是因为
每个td一个img
必须有两个tds…可能有两个原因:或者您有多行包含一个图像(选择器选择整个表格中的所有图像),或者代码运行了两次。很抱歉,这没有改变任何内容。关于图像标记:Als,只要这是HTML而不是XHTML,就可以了。是的,但正如我所说,只有一个图像。警报框告诉我来源,所以我知道是同一个。。