小码哥的IT人生

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

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024