【问题标题】:Is it possible to detect whether a jQuery click event has been called by actual click or trigger?是否可以检测实际单击或触发器是否调用了 jQuery 单击事件?
【发布时间】:2011-09-08 12:33:17
【问题描述】:

我有一些绑定到点击事件的功能:

$('.addButton').live('click', function(){  
 //stuff here...
});

有时通过实际的按钮单击调用该函数,有时使用trigger()。有没有办法检测出使用了哪种方法?

谢谢!

【问题讨论】:

    标签: jquery button triggers click


    【解决方案1】:

    看到这个:http://api.jquery.com/category/events/event-object/

    $('.addButton').live('click', function(e){ 
        if(e.which != undefined){
             //actual button click
        }
        else{
             //trigger
        }
    });
    

    在此处查看示例:http://jsfiddle.net/expertCode/d5SW2/

    【讨论】:

      【解决方案2】:

      我相信您可以将另一个包含 eventData 的参数传递给 .live

      .live( eventType, eventData, handler )
      // eventData A map of data that will be passed to the event handler.
      

      是否可以使用它来包含有关被叫方如何拨打电话的指示符?

      【讨论】:

        【解决方案3】:

        简短回答:是的。

        $('.addButton').live('click', function(event){  
        if(event.clientX === undefined)
          console.log("Invoked programatically")
        });
        

        当然,如果你愿意,你也可以仿效。但默认情况下不存在。

        【讨论】:

          【解决方案4】:

          您可以在触发函数中添加额外的参数,并使用 来区分可能的来源...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-05
            • 1970-01-01
            • 1970-01-01
            • 2010-10-09
            • 2011-09-06
            相关资源
            最近更新 更多