admin管理员组文章数量:1391981
I have this small gallery (the code is just example):
<div id="gallery_imgs">
<img src=".png" />
<img src=".png" />
<img src=".png" />
</div>
<p></p>
JS code:
var that = $("#gallery_imgs img:eq(1)");
$("p").html($("#gallery_imgs img").find("[src='"+that.attr("src")+"']").length);
This return 0. Any idea why? Is this a bug, or what am I doing wrong?
I have this small gallery (the code is just example):
<div id="gallery_imgs">
<img src="http://fc01.deviantart/fs71/f/2014/112/0/9/nobody_believes_in_me_by_idjpanda-d7fkd7m.png" />
<img src="http://fc09.deviantart/fs71/f/2014/112/5/6/feed_me_d__by_idjpanda-d7fgids.png" />
<img src="http://fc02.deviantart/fs71/f/2014/104/e/5/blar_auction_by_idjpanda-d7ehmoc.png" />
</div>
<p></p>
JS code:
var that = $("#gallery_imgs img:eq(1)");
$("p").html($("#gallery_imgs img").find("[src='"+that.attr("src")+"']").length);
This return 0. Any idea why? Is this a bug, or what am I doing wrong?
Share Improve this question asked Apr 23, 2014 at 16:19 kleniumklenium 2,6272 gold badges27 silver badges52 bronze badges2 Answers
Reset to default 5You need .filter()
$("p").html($("#gallery_imgs img").filter("[src='"+that.attr("src")+"']").length);
you can use .find() this way
$("p").html($("#gallery_imgs").find("img [src='"+that.attr("src")+"']").length);
Problem
$("#gallery_imgs img").find("[src='"+that.attr("src")+"']")
you are trying to find the element with src
inside the img
element not within in the img
element
Try
$("p").html($("#gallery_imgs img[src="+that.attr("src")+"]").length);
本文标签: javascriptjquery find element by src attributeStack Overflow
版权声明:本文标题:javascript - jquery find element by src attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744774585a2624548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论