HTML DOM click() 方法
JavaScript基础 2022-06-08 16:00:27小码哥的IT人生shichen
HTML DOM click() 方法
定义和用法
click() 方法可模拟在按钮上的一次鼠标单击。
语法
buttonObject.click()
实例
下面的例子将在 body onload 上模拟在按钮上的一次鼠标单击:
<html>
<head>
<script type="text/javascript">
function clickButton()
{
document.getElementById('button1').click()
}
function alertMsg()
{
alert("Button 1 was clicked!")
}
</script>
</head>
<body onload="clickButton()"
>
<form>
<input type="button" id="button1" onclick="alertMsg()"
value="Button 1" />
</form>
</body>
</html>
TIY
完整实例【模拟在按钮上的一次鼠标单击】:
<html>
<head>
<script type="text/javascript">
function clickButton()
{
document.getElementById('button1').click()
}
function alertMsg()
{
alert("Button 1 was clicked!")
}
</script>
</head>
<body onload="clickButton()">
<form>
<input type="button" id="button1" onclick="alertMsg()" value="Button 1" />
</form>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html