Question

I have this small gallery (the code is just example):

<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 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?

Était-ce utile?

La solution

You 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

Autres conseils

Try

$("p").html($("#gallery_imgs img[src="+that.attr("src")+"]").length);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top