Replace all PNGs with GIFs in jquery?
Is it possible to replace开发者_运维百科 all PNGs across the board with GIFs using jquery? How would I do this?
Thanks
Do you have the images as gifs already?
If you do, it will be as easy as doing something like:
$('img').each(function() {
var src = $(this).attr("src").replace(".png", ".gif");
$(this).attr("src", src);
});
This will go through all the img tags, and replace the path with it's own path, but with the gif extension instead of png
Let me know if that helps you.
Are all PNGs referenced as '.png'? If not, it isn't possible with JS alone (see Mozilla's doc on the Image object: No property re. the mime type).
Else try mplacona's solution.
Edit: To give an example: This URL
http://www.w3.org/Icons/valid-html40
leads to an image, but you cannot guess, which format it is. You can explicitly request
http://www.w3.org/Icons/valid-html40.gif
or
http://www.w3.org/Icons/valid-html40.png
but, without extension, the server decides, what it sends, and the client (JavaScript, that is) has no means to decide what will come.
精彩评论