【问题标题】:bootstrap popover add esc keypress check and closebootstrap popover 添加 esc 按键检查并关闭
【发布时间】:2012-04-04 00:39:32
【问题描述】:

我正在尝试扩展引导弹出窗口以在手动模式下退出时关闭。我正在尝试扩展 popover 类继承的工具提示类,如下所示:

/* 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
  }
}

【问题讨论】:

    标签: jquery closures twitter-bootstrap popover


    【解决方案1】:

    你需要使用这个:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-02
      • 2015-10-24
      • 2014-08-30
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      相关资源
      最近更新 更多