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');
您也可以使用纯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.