gpt4 book ai didi

javascript - 使用 JavaScript 设置 alt 标签

转载 作者:行者123 更新时间:2023-11-28 04:09:15 24 4
gpt4 key购买 nike

我知道如何在 <img> 上设置 ALT 标签如果我对页面上的所有图像都有类或全局,但是如何对没有 id 或类的特定图像执行此操作?我能否以某种方式使用父 DIV 来引用图像并添加 alt 标签?

<div id="table_processing">
<img src="processing_report.gif"> Loading Report List...
</div>

最佳答案

我建议使用 document.querySelector(),它允许您使用 CSS(类似 jQuery)选择器来选择图像。

像这样:

var image = document.querySelector("#table_processing img");

然后您可以设置 alt 属性:

image.alt = "Our New Alt Text";

image.setAttribute("alt", "our New Alt Text");

这是一个演示:

var image = document.querySelector("#table_processing img");

image.alt = "Our New Alt Text";
<div id="table_processing">
<img src="http://via.placeholder.com/350x150"> Loading Report List...
</div>

关于javascript - 使用 JavaScript 设置 <img> alt 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47943901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com