jQuery每次迭代问题

发布于 2024-10-03 08:35:20 字数 852 浏览 0 评论 0原文

<section class="photos">
    <table>
        <tr>
            <td><a href="/a"><img src="b.jpg" /></a></td>
            <td><a href="/a"><img src="b.jpg" /></a></td>
            <td><a href="/a"><img src="b.jpg" /></a></td>
        </tr>
        <tr>
            <td><a href="/a"><img src="b.jpg" /></a></td>
            <td><a href="/a"><img src="b.jpg" /></a></td>
        </tr>
    </table>
</section>

然后我尝试迭代每个以返回链接 href 和图像 src。

$(".photos td").each(function (index, value) {
    console.log(value.a.attr('href'));
    console.log(value.img.attr('src'));
});

不幸的是,上面的伪代码不起作用。有什么想法吗?

<section class="photos">
    <table>
        <tr>
            <td><a href="/a"><img src="b.jpg" /></a></td>
            <td><a href="/a"><img src="b.jpg" /></a></td>
            <td><a href="/a"><img src="b.jpg" /></a></td>
        </tr>
        <tr>
            <td><a href="/a"><img src="b.jpg" /></a></td>
            <td><a href="/a"><img src="b.jpg" /></a></td>
        </tr>
    </table>
</section>

And then I am trying to iterate through each to return the link href and the image src.

$(".photos td").each(function (index, value) {
    console.log(value.a.attr('href'));
    console.log(value.img.attr('src'));
});

The above pseudo-broken-code doesn't unfortunately work. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-10-10 08:35:20

您无法在 value 上运行 jQuery 方法,因为它只是一个 DOM 节点。首先用它创建一个 jQuery 对象(我更喜欢 this 而不是 value 参数,但在本例中它们是相同的):

$('.photos td').each(function(){
    console.log( $(this).find('a').attr('href') );
    console.log( $(this).find('img').attr('src') );
});

You can't run jQuery methods on value, since it's just a DOM node. Create a jQuery object out of it first (I prefer this to the value argument, but they're the same in this case):

$('.photos td').each(function(){
    console.log( $(this).find('a').attr('href') );
    console.log( $(this).find('img').attr('src') );
});
不爱素颜 2024-10-10 08:35:20

Adam 已经给出了答案,但是既然我正在输入它......试试这个:(唯一的区别在于遍历部分,因为 Adam 使用“this”而我使用“value”(它们是相同的)。

$(".photos td").each(function (index, value) {
    console.log($('a', value).attr('href'));
    console.log($('img', value).attr('src'));
});

不要忘记 value 代表 dom 元素(而不是 jQuery 元素),并且 attr() 是仅适用于 jQuery 对象的 jQuery 方法。
另外,你不应该关闭你的标签吗?

Adam already gave the answer, but since I was typing it... try this: (the only difference is in the traversal part, since Adam uses "this" while I use "value" (which are the same).

$(".photos td").each(function (index, value) {
    console.log($('a', value).attr('href'));
    console.log($('img', value).attr('src'));
});

Don't forget that value represents a dom element (and not a jQuery element), and attr() is a jQuery method that works only for jQuery objects.
Also, shouldn't you be closing your tags?

云淡风轻 2024-10-10 08:35:20

试试这个:

$(".photos td").each(function (index, value) {
    console.log($(this).find('a').attr('href'));
    console.log($(this).find('img').attr('src'));
});

Try this:

$(".photos td").each(function (index, value) {
    console.log($(this).find('a').attr('href'));
    console.log($(this).find('img').attr('src'));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文