javascript 在javascript中为图像添加alt属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15471688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:54:02  来源:igfitidea点击:

Adding alt attribute to image in javascript

javascriptimagealt

提问by mukund

I want to know how to add alt attribute to an image in javascript, below is my code...

我想知道如何在 javascript 中向图像添加 alt 属性,下面是我的代码...

var image = document.createElement('img');
image.src='../../KY/images/common/buttons/browseIcon.png';

Thanks in advance.

提前致谢。

回答by Bigood

You have two ways : you can use either altproperty

您有两种方法:您可以使用任一alt属性

image.alt = "Your text here"

or setAttributemethod

setAttribute方法

image.setAttribute("alt","Your text here");

回答by Karl Johan

You could just use the alt property.

你可以只使用alt 属性

回答by umar

Above mentioned methods would not work when u create buttons or any other element in dom javascript, and for this use the below mentioned method

当您在 dom javascript 中创建按钮或任何其他元素时,上述方法将不起作用,为此使用下面提到的方法

control.setAttribute("title","your tooltip value")