案例 <input type="button" value="Click" id="C">
<input type="button" value="Wait" id="W">
<script>
var C=document.getElementById("C");
var W=document.getElementById("W");
function Go(){
W.click();
}
</script>
给按钮2添加点击事件click触发按钮1的点击事件,就算给按钮1添加样式display: none;或visibility: hidden;隐藏起来也能触发。 <button type="button" id="btn1" >按钮1</button>
<button type="button" id="btn2">按钮2</button>
<script type="text/javascript">
const BTN1 = document.getElementById('btn1');
const BTN2 = document.getElementById('btn2');
//给按钮2添加点击事件
BTN2.addEventListener('click', () => {
BTN1.click(); //按钮2点击后触发按钮1的onclick
});
</script>
|