一个按钮上的多操作与javascript例程

Multi action on one button with javascript routine

本文关键字:操作 javascript 例程 一个 按钮      更新时间:2023-09-26

当使用 javascript 按下按钮时,如何在两个(或三个)对象上设置"单击操作"?非常感谢

您可以在按钮onclick事件中调用其他可点击元素click()方法,例如

<input id="Button1" type="button" value="Button 1" onclick="clickAllbuttons()" />
<input id="Button2" type="button" value="Button 2" onclick="alert('Button 2 is clicked!')" />
<input id="Button3" type="button" value="Button 3" onclick="alert('Button 3 is clicked!')"/>
 <script type="text/javascript">
     function clickAllbuttons() {
         alert('Button 1 is clicked!')
         document.getElementById('Button2').click();
         document.getElementById('Button3').click();
     }
 </script> 

这里 3 个按钮是在 HTML 标记中定义的,但如果您单击按钮 1 - 按钮 2 和按钮 3 也会被单击。