简体   繁体   中英

how to set imge alt attribute by its div id by using javascript

I am trying to set image alt attribute via JavaScript.The problem is that image do not have id it self, All I have of its div Id.

<div id="logo">
<a href="http://yourwebsite.com">
<img alt="nkki" src="http://crankit.com.au/~thstarse/wp-content/themes/bigbangwp/image/logo.png">
</img>
</a>

What I am trying, however I am having no luck. Can anyone give me a hand with my code?

$(this).children('img').setAttribute('alt', 'new alt');

The Answer of this question is

document.querySelector("#logo img").setAttribute('alt', 'new alt');

Try this

$('#logo').find('img').attr('alt', 'new alt');

Working Demo

您也可以使用纯JavaScript来实现。

document.querySelector("#logo img").setAttribute('alt', 'new alt');

你可以试试这个演示

$("#logo img").attr("alt","alertnate image.jpg");

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