如何根据图像的路径/URL选择图像? [英] How do I select an image based on its path/url?

查看:86
本文介绍了如何根据图像的路径/URL选择图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据图片的URL选择图片,但是由于某种原因,它没有发挥作用:

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']");

抛出此错误:"TypeError:对象不支持此属性或方法".

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']");

我没有退回任何物品.我已经在运行时直接从生成的html复制并粘贴了路径,但它仍然拒绝返回任何项目.如果我将src选择器替换为id选择器,它将返回项目.

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;" />

我正在运行jQuery 1.4.2,并且已经检查了所有文档,并且所有似乎都已正确编码.我假设这不是错误,而是我的误解.她能对此有什么看法吗?

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?

欢呼

推荐答案

请勿使用@,并确保也包含完整的查询.如果有的话.

Don't use the @ and be sure to include the full query too. If there is one.

$("img[src='/images/marketing/logo.png?1277792289']")[0]

您也可以进行子字符串匹配

You can do substring matching as well

$("img[src*='logo']")[0]

:not选择器在错误的位置.它不应放在属性括号中.

And the :not selector is in the wrong place. It should not be in the attribute brackets.

$("img[src*='logo']:not(:visible)")[0]

这篇关于如何根据图像的路径/URL选择图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆