2012-04-04 15 views
2

マニュアルモードでエスケープするときにブートストラップポップオーバーを拡張しようとしています。私は、ツールチップのクラスに以下のようなそのポップオーバークラスinhertsを拡張しようとしています:bootstrap popover add esc keypress check and close

/* 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には関数のスコープがあることを覚えておいてください。これは、さまざまな関数の中で "this"変数について慎重にする必要があることを意味します。

関連する問題