最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery addclass if image has specific alt tag - Stack Overflow

programmeradmin2浏览0评论

I have some images, all of which have alt tags.

I am trying to add a class to all of the images which have a certain alt tag.

So to put it in plain terms:

If image alt tag = "dog" then add class "canine"

Any ideas?

thanks

I have some images, all of which have alt tags.

I am trying to add a class to all of the images which have a certain alt tag.

So to put it in plain terms:

If image alt tag = "dog" then add class "canine"

Any ideas?

thanks

Share Improve this question edited Dec 7, 2010 at 14:24 Gabriele Petrioli 196k34 gold badges271 silver badges328 bronze badges asked Dec 7, 2010 at 14:22 TomTom 13k50 gold badges153 silver badges247 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

You can use the attribute equals selector and addClass method:

$("img[alt='dog']").addClass("canine");

or you can use

$("img[alt*='dog']").addClass("canine");

to include any image with alt Containing dog.

$('img[alt="dog"]').addClass("canine")

it's realy easy...

notetoself: type faster... ;)

发布评论

评论列表(0)

  1. 暂无评论