JavaScript click event-one button triggers another button

<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 Add to button click event clicktrigger button click event 1, even add style to the button 1 display: none;or visibility: hidden;hide can also trigger.

<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>

On the blog , articles related to this article:

" Bootstrap Learning-Menu-Button-Navigation "
" CSS pseudo-class selector active to simulate JavaScript click events "
" JavaScript makes input[type=text] prohibit input but keep the cursor through preventDefault() "

Copyright Notice

All original articles on this blog, the author reserves the copyright. Reproduced must include this statement, keep the article intact, and the form of hyperlinks indicate the author after the addition and the original article address:https://blog.mazey.net/602.html

Guess you like

Origin blog.51cto.com/mazey/2643031