我想在javascript中检测触发的点击事件,我的代码是
$(".my_button").click(function(){
// if event fired form .trigger() method from other method or event then do something
// actuall code for click event is here
})
.....
.....
$('.other_first_button').click(function(){
// code of other first button
$(".my_button").trigger('click');
});
.....
.....
$('.other_second_button').click(function(){
// code of other second button
$(".my_button").trigger('click');
});
注意:我想在trigger()
中使用jquery $(".my_button").click()
方法识别点击事件调用。
提前感谢您的时间。
答案 0 :(得分:1)
将参数作为
传递给触发点击事件$(".my_button").trigger('click', ['btn2']);
点击我的按钮上的点击事件
$(".my_button").click(function(e, p2){
不检查p2是否未定义或具有某个值,如果它已使用trigger()
事件触发
$(".my_button").click(function(e, p1){
// if event fired form .trigger() method from other method or event then do something
if(p1 != undefined)
alert('triggered from ' + p1);
// actuall code for click event is here
})
$('.other_first_button').click(function(){
// code of other first button
$(".my_button").trigger('click', ['btn1']);
});
$('.other_second_button').click(function(){
// code of other second button
$(".my_button").trigger('click', ['btn2']);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="other_first_button">
First
</button>
<button class="other_second_button">
Second
</button>
<button class="my_button">
mybtn
</button>