yfisaboy 2020-07-28
<input type="button" value="Click" id="C" onclick="Go();">
<input type="button" value="Wait" id="W" onclick="javascript:alert(‘Amazing!‘);">
<script>
var C=document.getElementById("C");
var W=document.getElementById("W");
function Go(){
W.onclick();
}
</script>
<input type="button" value="Click" id="C" onclick="Go();">
<input type="button" value="Wait" id="W" onclick="javascript:alert(‘Amazing!‘);">
<script>
var C=document.getElementById("C");
var W=document.getElementById("W");
function Go(){
W.onclick();
}
</script>
给按钮2添加点击事件click
触发按钮1的点击事件,就算给按钮1添加样式display: none;
或visibility: hidden;
隐藏起来也能触发。
<button type="button" id="btn1" onclick="alert(‘我是按钮1的弹框‘)" >按钮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.onclick(); //按钮2点击后触发按钮1的onclick //BTN1.click(); //按钮2点击后触发按钮1的click 效果一样 }); </script>