开发者

Finding images on page that have a specific filename with jquery

开发者 https://www.devze.com 2023-01-23 13:49 出处:网络
I need to do a check on a page to check if certain images have loaded ie. not returned 404. If they arent available I want to replace the image with a default images.

I need to do a check on a page to check if certain images have loaded ie. not returned 404. If they arent available I want to replace the image with a default images.

I am aware I can assign an onerror handler to the images.

Firstly I need to use a selector to find all the images that have a source which contains the following image name -> /folder/Product_1234_P.jpg OR /folder/开发者_高级运维Product_9876_Q.gif

Any ideas how I can create a high performance selector that I can use to find such images so that I can check if the images loaded, if not replace the image name with: Product_Default_P.jpg or Product_Default_Q.gif

Any help or advice would be great.


You can use the image's error handler and the attribute contains selector like so:

$("img[src*=Product]").error(function() {
    $(this).attr('src', 'notfound.jpeg');
});


$('img[src*="substring"]')

It's an attribute selector. Check it out http://api.jquery.com/attribute-contains-selector/


I guess you mean something like

$("img[src^=/folder/Product_][src$=_P.jpg]")

i.e. "Find all img with a src starting with "/folder/Product_" and ending with "_P.jpg".

0

精彩评论

暂无评论...
验证码 换一张
取 消