【问题标题】:Jquery - Get current eventJquery - 获取当前事件
【发布时间】:2014-01-03 16:58:29
【问题描述】:

我知道我可以通过将事件作为函数的参数传递来获取事件:

$(".selector").on("click", function(event) {


});

如果不是函数的参数怎么获取呢?

例如,

$(".selector").on("click", function() {

// var event = ???

});

【问题讨论】:

  • 你为什么不把它作为参数?有什么原因吗?
  • 因为我无法重写函数。
  • 如果你使用事件变量,这会影响什么吗?尝试使用这个关键字访问..

标签: jquery


【解决方案1】:

即使没有声明参数,它仍然是传递给函数的,所以你可以这样写:

$(".selector").on("click", function() {
    var evt = arguments[0];
});

【讨论】:

  • 在更一般的情况下,我发现我可以通过这种方式捕获事件:function MyFunc() { var evt = window.event || arguments.callee.caller.arguments[0] }
  • @enb081,注意arguments.callee在严格模式(there are workarounds, though)下不受支持。
【解决方案2】:
$(".myform .form-control").on("input blur",function(e){

    console.dir(arguments[0].handleObj.type);

});

此代码可以给出事件的当前名称

希望对你有帮助:)

【讨论】:

    【解决方案3】:

    只需使用下面的代码。

    $(".selector").on("click", function(event) {
      console.log(event.type); //output = click
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      相关资源
      最近更新 更多