HTML DOM alt 属性
JavaScript基础 2022-06-08 16:19:59小码哥的IT人生shichen
HTML DOM alt 属性
定义和用法
alt 属性可设置或返回在浏览器不支持单选按钮时显示的替代文本。
语法
radioObject.alt=alternate_text
实例
下面的例子可返回单选按钮的 alt 文本:
<html>
<body>
<form>
Male: <input id="male" alt="male" type="radio"
name="Sex" value="Male" />
<br />
Female: <input id="female" alt="female" type="radio"
name="Sex" value="Female" />
</form>
<p>The alt text for the radio buttons are:
<script type="text/javascript">
document.write(document.getElementById('male').alt
);
document.write(" and ");
document.write(document.getElementById('female').alt
);
</script></p>
</body>
</html>
TIY
完整实例【返回单选按钮的 alt 文本】:
<html>
<body>
<form>
Male: <input id="male" alt="male" type="radio" name="Sex" value="Male" />
<br />
Female: <input id="female" alt="female" type="radio" name="Sex" value="Female" />
</form>
<p>The alt text for the radio buttons are:
<script type="text/javascript">
document.write(document.getElementById('male').alt);
document.write(" and ");
document.write(document.getElementById('female').alt);
</script></p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html