2012-04-04 82 views
2

我试图扩展引导弹窗以关闭手动模式下的转义。我试图扩展工具提示类像其酥料饼类inherts以下:bootstrap popover添加esc按键检查并关闭

/* WHY CANT I CALL the HIDE FUNCTION WHEN ESC key press is intercepted???? 

    Why is the hide class undefined when the keypress is intercetped? 
*/ 

!function ($) { 

    "use strict" 

    /* TOOLTIP PUBLIC CLASS DEFINITION 
    * =============================== */ 

    var Tooltip = function (element, options) { 
     this.init('tooltip', element, options) 
    } 

    Tooltip.prototype = { 

     constructor: Tooltip 

    , init: function (type, element, options) { 
     //init logic here 

     $(document).keypress(function (e) { 
      if (e.which == 27) { this.hide }; 
     });     ^^^^^^^^^^^^^ 
          this.hide is undefined on debug???? 
    } 

    , hide: function() { 
    //hide logic 
    } 
} 

回答

4

您需要使用这样的:

$tooltip = this; 
$(document).keydown(function(e){ 
    if (e.keyCode === 27) 
     $tooltip.hide(); 
}); 

你的问题是,“这个”你想实际上是文件,它没有隐藏功能。当keypress/keydown事件被触发时,函数内的“this”就是文档触发事件的元素。记住JavaScript的功能范围,这意味着当你在许多不同的功能中时,你需要对你的“这个”变量保持谨慎。