How do I detect if an image is a png with jQuery?
I need some way to mar开发者_如何学Gok PNGs with jQuery. Is there any way to do this?
In order to select image elements with a png extension in jQuery, you can use attribute selectors.
For your question, you want to match any img
element with a src
attribute that ends with ($=
) .png
, so your solution would be $("img[src$='.png']")
. If you want to mark those items with a specific class (say, png
), you would do: $("img[src$='.png']").addClass('png');
You can use the same style to select elements with any attribute you desire. For instance, to find all anchors with a hash value for the href
attribute, you can use: $("a[href^='#']")
jQuery has a big list of different types of selectors, which you can read about here: http://api.jquery.com/category/selectors/
精彩评论