HTML DOM alt 属性
JavaScript基础 2022-06-08 15:57:0216小码哥的IT人生shichen
HTML DOM alt 属性
定义和用法
alt 属性可设置或返回图像无法显示时的替代文本。
语法
imageObject.alt=alternate_text
实例
下面的例子将更改图像的替代文本:
<html> <head> <script type="text/javascript"> function changeAlt() { document.getElementById("compman").alt="Man using computer"
} function alertAlt() { alert(document.getElementById("compman").alt
) } </script> </head> <body> <img id="compman" src="compman.gif" alt="Computerman" /> <br /> <input type="button" onclick="changeAlt()" value="Change alt text"> <br /><br /> <input type="button" onclick="alertAlt()" value="Alert alt text"> </body> </html>
TIY
完整实例【更改图像的替代文本】:
<!DOCTYPE html> <html> <body> <img id="myImg" src="/i/shanghai_lupu_bridge.jpg" alt="上海卢浦大桥" width="350" height="234"> <p>单击按钮以显示图像的替代文本。</p> <button onclick="myFunction()">试一试</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myImg").alt; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html