单击一个按钮以触发另一个按钮的点击事件 [英] Clicking on one button to trigger click event on another
本文介绍了单击一个按钮以触发另一个按钮的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想单击按钮2来触发按钮1上的点击事件.
I want to click on button 2 to trigger a click event on button 1.
但是,当我尝试以下操作时,单击#2不会发生任何事情:#1或#2没有警报.
However, when I try the following, nothing happens when clicking on #2: no alert for #1 or #2.
HTML:
<div id="container">
<button id="button-1">Button 1</button>
<button id="button-2">Button 2</button>
</div>
JS:
$('#container').on( "click", '#button-1', function(e){
alert('CLICKED 1');
});
$('#container').on( "click", '#button-2', function(e){
$('#button-1').trigger(e);
alert('CLICKED 2');
});
http://jsfiddle.net/8RnBf/7/
此外,如果将#2警报放在触发器之前,则会导致无限循环.
Moreover, if I put the #2 alert before the trigger, I end up with an infinite loop.
http://jsfiddle.net/8RnBf/6/
为什么这不能正常工作?
Why is this not working as expected?
更新:
我应该明确指出:必须传递原始事件.从本质上讲,我们正在尝试 此处 ,但要进行委派事件
I should have made it clear: the original event must be passed. In essence, we're trying to do what was done here, but with delegated events
推荐答案
使用
$('#button-1').trigger('click');
或
$('#button-1').trigger(e.type);
代替
$('#button-1').trigger(e);
这篇关于单击一个按钮以触发另一个按钮的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文