代码之家  ›  专栏  ›  技术社区  ›  Dave Taylor

在javascript中,如何在已处理的键盘事件中引用调用对象

  •  0
  • Dave Taylor  · 技术社区  · 16 年前

    您好,我正在编写一个javascript小部件,用于处理键盘事件。当我显示一个div并想在有人按下esc时隐藏它时,就会出现问题。

    var escToExit = function(e){
       // code to check for esc
      // i then want to call the instance of widget that is linked to this function
    }
    var widget = {
      show : function(){
        $(document).keyup(escToExit);
      },
      hide : function(){
        //hide code here
      }
    }
    

    谢谢

    1 回复  |  直到 16 年前
        1
  •  0
  •   BenMorel Manish Pradhan    12 年前

    编辑

    var escToExit = function(e){
      e.data.wpass; // here is your ref
      // rest of func
    }
    
    var widget = {
      show : function() {
        $(document).bind('keyup', {wpass : widget}, escToExit);
      // rest of obj
    }
    

    看见

    http://api.jquery.com/bind/

    http://api.jquery.com/trigger/