jQuery通过src属性查找元素 [英] jquery find element by src attribute
本文介绍了jQuery通过src属性查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小画廊(代码只是示例):
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代码:
var that = $("#gallery_imgs img:eq(1)");
$("p").html($("#gallery_imgs img").find("[src='"+that.attr("src")+"']").length);
此返回0.为什么?这是一个错误,还是我做错了什么?
This return 0. Any idea why? Is this a bug, or what am I doing wrong?
推荐答案
您需要 .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")+"']")
,您正在尝试查找img
元素内img
元素
$("#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
这篇关于jQuery通过src属性查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文