简体   繁体   English

检查图像是否存在+加载图像而不知道其全名

[英]check if an image exists + loading an image without knowing the whole name of it

  • how can I check if an image exist via javascript. 如何通过javascript检查图像是否存在。
  • is it possible to load an image called 145_44.jpg and I only know the first 3 numbers 145 ? 是否可以加载名为145_44.jpg的图像,而我只知道前3个数字145

The html image element has onerror and onload events that will fire if loading the image fails or succeeds respectively. html图像元素具有onerror和onload事件,如果分别加载图像失败或成功,则将触发它们。

With regards to loading an image without knowing the correct URI, the answer is no. 关于在不知道正确URI的情况下加载图像,答案是否定的。 Its up to the web server to decide what to serve in response to an HTTP request. 由Web服务器决定响应HTTP请求提供服务的内容。 If the request is for a resource it doesn't know about its very unlikely to serve anything other than a 404 error. 如果请求是针对资源的,则除了404错误外,它不大可能会提供其他服务。

I'm unsure if you'll be able to link to an image without knowing the full name of it, however with the new CSS3 '*" selector you might be able to. 我不确定您是否可以在不知道图像全名的情况下链接到图像,但是使用新的CSS3'*“选择器,您也许可以。

example: 例:

    <img src="*145*.jpg" /> 

this might be able to catch it (i'm at work so i can't test) but give it a shot. 这也许可以捕捉到它(我正在工作,所以我无法测试),但可以试一试。 the * replaces everything before/after it telling the browser to search for anything with the number 145 in it that is a .jpg *替换之前/之后的所有内容,告诉浏览器搜索其中带有数字145的任何内容(即.jpg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM