HTML DOM id 属性
JavaScript基础 2022-06-08 16:20:26小码哥的IT人生shichen
HTML DOM id 属性
定义和用法
id 属性可设置或返回单选按钮的 id。
语法
radioObject.id=id
实例
下面的例子可提示出单选按钮的 id:
<html>
<body>
<form>
<input type="radio" id="radio1" />
</form>
<p>The id of the radio button is:
<script type="text/javascript">
x=document.getElementById('radio1');
document.write(x.id
);
</script>
</p>
</body>
</html>
TIY
完整实例【显示出一个单选按钮的 id、类型和值】:
<html>
<head>
<script type="text/javascript">
function alertId()
{
var txt="Id: " + document.getElementById("male").id
txt=txt + ", type: " + document.getElementById("male").type
txt=txt + ", value: " + document.getElementById("male").value
alert(txt)
}
</script>
</head>
<body>
<form>
Male: <input id="male" type="radio" name="Sex" value="Male" />
<br />
Female: <input id="female" type="radio" name="Sex" value="Female" />
<br /><br />
<input type="button" value="Alert details" onclick="alertId()" />
</form>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html