javascript - jquery 通过 src 属性查找元素

标签 javascript jquery jquery-selectors

我有这个小画廊(代码只是示例):

<div id="gallery_imgs">
    <img src="http://fc01.deviantart.net/fs71/f/2014/112/0/9/nobody_believes_in_me_by_idjpanda-d7fkd7m.png" />
    <img src="http://fc09.deviantart.net/fs71/f/2014/112/5/6/feed_me_d__by_idjpanda-d7fgids.png" />
    <img src="http://fc02.deviantart.net/fs71/f/2014/104/e/5/blar_auction_by_idjpanda-d7ehmoc.png" />
</div>
<p></p>

JS代码:

var that = $("#gallery_imgs img:eq(1)");
$("p").html($("#gallery_imgs img").find("[src='"+that.attr("src")+"']").length);

这会返回 0。知道为什么吗?这是一个错误,还是我做错了什么?

最佳答案

你需要.filter()

$("p").html($("#gallery_imgs img").filter("[src='"+that.attr("src")+"']").length);

你可以使用.find()这样

$("p").html($("#gallery_imgs").find("img [src='"+that.attr("src")+"']").length);

问题

$("#gallery_imgs img").find("[src='"+that.attr("src")+"']") 您正在尝试查找元素srcimg 元素内而不在 img 元素内

关于javascript - jquery 通过 src 属性查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23250033/

相关文章:

jQuery 非选择器和实时绑定(bind)

javascript - JQuery 选择由 2 个特定单词组成的类名

javascript - Angular js,关于ng-change如何更新其他选择控件的选项

javascript - 如何使用ajax(laravel)从mysql数据库获取数据?

javascript - 如何添加 plyr.io 下载按钮和进度缩略图

javascript - 使用ajax使用php更新mysql表

javascript - 加载动态内容后调用函数

javascript - 使用 momentjs 检测无效时区

php - 完成后使用 jQuery 向 iframe 反馈提交表单?

jQuery 选择除第一个以外的所有