我需要通过名称和源找到一个img。我一直在尝试以下方法,但都无济于事。
var s = $("img[src='/images/greendot.gif'][name='BS']");
html:
<img alt="This item is active." name="BS" src="/images/greendot.gif"/>
vs
<img alt="This item is not active." name="BS" src="/images/spacer.gif"/>
发布于 2009-05-07 15:50:55
在没有看到html的情况下,我会说检查您的路径。
$("img[src$='greendot.gif'][name='BS']")
给定以下HTML:
<img src="http://assets0.twitter.com/images/twitter_logo_header.png" name="logo" />
这个jquery起作用了:
var x = $("img[src$='twitter_logo_header.png'][name='logo']");
alert(x.attr("src"));
发布于 2009-05-07 15:42:31
试着去掉引号:
var s = $("img[src=../../images/greendot.gif][name=BS]");
发布于 2011-04-29 02:01:25
浏览器将完整路径添加到src、href和类似的属性。因此,尽管元素是用相对路径定义的,但在DOM的“src”属性中包含域名和完整路径。使用src$="...“语法或指定完整域名似乎是使用jQuery通过src查找图像的唯一选项。
在这一点上我可能错了。image对象的src属性报告完整路径,但该属性应包含代码中指定的路径。在最新的jQuery版本中,src="relative/path“可以工作,也许它是一个早期版本,它不能工作。
https://stackoverflow.com/questions/835378
复制相似问题