如何根据路径/URL 选择图像?
我想根据它们的 URL 选择图像,但由于某种原因它没有发挥作用:
最终我追求的是这样的东西:
var imgs = $("img[@src='images/object.png']:not(:hidden)");
但即使有简单的东西,比如:
$("img[@src='images/object.png']");
抛出此错误:“TypeError:对象不支持此属性或方法”。
如果我在查询中省略@:
$("img[src='images/object.png']");
我不会返回任何项目。我已经在运行时直接从生成的 html 中复制并粘贴了路径,但它仍然拒绝返回任何项目。如果我用 id 选择器替换 src 选择器,它会返回项目。
这是在运行时生成的图像标签:
<img id="ctl00_ContentPlaceHolder1_object_1" src="images/object.png" style="height:16px;width:16px;border-width:0px;visibility:visible;display:inline;margin-right:3px;" />
我正在运行 jQuery 1.4.2,并且我已经检查了所有文档,并且所有文档似乎都已正确编码。我认为这不是一个错误,而是我的误解。任何人都可以对此有所了解吗?
干杯
I'm tying to select images based on their URLs, but for some reason it's not playing ball:
Ultimately I'm after something like:
var imgs = $("img[@src='images/object.png']:not(:hidden)");
But even with something simple like:
$("img[@src='images/object.png']");
This error is thrown: "TypeError: Object doesn't support this property or method".
If I omit the @ from the query:
$("img[src='images/object.png']");
I get no items returned. I've copied and pasted the path directly from the generated html at runtime and it still refuses to return any items. If I replace the src selector with an id selector, it returns the items.
This is the image tag generated at runtime:
<img id="ctl00_ContentPlaceHolder1_object_1" src="images/object.png" style="height:16px;width:16px;border-width:0px;visibility:visible;display:inline;margin-right:3px;" />
I'm running jQuery 1.4.2 and I've checked all the documentation and all appears to be coded correctly. I'm assuming this isn't a bug, but a misinterpretation on my part. Can anyone she any light on this?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
@
并确保也包含完整的查询。如果有的话。您也可以进行子字符串匹配,
并且
:not
选择器位于错误的位置。它不应该在属性括号中。Don't use the
@
and be sure to include the full query too. If there is one.You can do substring matching as well
And the
:not
selector is in the wrong place. It should not be in the attribute brackets.这曾经是 jQuery 中的一个错误,显然现在仍然如此。
查看以下票证:
本质上,如果您使用 attributeEndsWith ($) 或 attributeContains (* ) 选择器,因为 jQuery 不会将您的字符串与您放入图像 src 属性的内容进行比较,而是与完整的 url 路径(即 http:// ..... /images/object.png)进行比较
This used to be a bug in jQuery and apparently it still is.
Check out the following tickets:
Essentially, what you have will work if you use the attributeEndsWith ($) or attributeContains (*) selectors because jQuery is not comparing your string against exactly what you put into the image src attribute, but against the full url path (ie, http:// ..... /images/object.png)