简体   繁体   中英

How to add an alt attribute of img tags with jquery

I've put a google map in a page and it makes lots of img tags without the alt attribute, how can I add the alt for each img in a div in jquery, what's I gotta make instead of :

$('#map_canvas > img[alt]').attr('image');

Thanks

To set alternative text to all the images:

$('#map_canvas > img').attr('alt', 'Alternative text');

To set alternative text to images that does not have alt attribute:

$('#map_canvas > img:not([alt])').attr('alt', 'Alternative text');

尝试:

$("#map_canvas > img:not([alt])").attr("alt", "<YOURALTTEXT>");

这项工作:

$('#map_canvas').find('img:not([alt])').attr('alt', 'Alt text');

You could do:

$('#map_canvas > img[alt=""]').each(function()
{
    $(this).attr('alt', $(this).attr('src');
});

This would find all images within #map_canvas that do not have an alt tag, and then set the alt tag to be the same as the image src .

Alternatively, I would recommend:

$('#map_canvas > img[alt=""]').attr('alt', 'Alt text');

As this will only add alt tags to images that don't have one.

$('#map_canvas > img').attr('alt', 'alt_text');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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