js 模拟触发 按钮的 onclick 博客分类: jsp js
程序员文章站
2024-03-21 18:37:40
...
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
</title>
<script type="text/javascript">
function ol(){
alert("yang");
}
onload = function(){
var btn = document.getElementById("myBtn");
var event = document.createEvent("MouseEvents");//鼠标事件对象
event.initMouseEvent("click");//具体鼠标事件
btn.dispatchEvent(event);//分发
}
</script>
</head>
<body>
<input type="button" value="click me" id="myBtn" onclick="ol();"/>
</body>
</html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
</title>
<script type="text/javascript">
function ol(){
alert("yang");
}
onload = function(){
var btn = document.getElementById("myBtn");
var event = document.createEvent("MouseEvents");//鼠标事件对象
event.initMouseEvent("click");//具体鼠标事件
btn.dispatchEvent(event);//分发
}
</script>
</head>
<body>
<input type="button" value="click me" id="myBtn" onclick="ol();"/>
</body>
</html>