【问题标题】:Bootstrap popover with manual trigger attached on dynamic content带有手动触发器的引导弹出窗口附加在动态内容上
【发布时间】:2015-01-29 13:59:03
【问题描述】:

我有一组动态的 contenteditable div。具有类“.showPopover”的 div 将有一个弹出窗口。弹出框触发器设置为手动,因为我希望它们显示在焦点上,但并不总是隐藏在模糊中。

我在这里找到[问题]:Bootstrap Tooltip with manual trigger and selector option 我不能将“选择器方法”与手动触发器一起使用,所以我遵循了那里的答案之一,但是对于动态添加的 div,仍然没有出现弹出框。

问题是,我只想为具有特定类的 div 显示弹出框,而不是与 div 一起添加。

弹出框的 div 类的更改通过启用按钮进行了一些简化。

jQuery(document).ready(function($) {

$('a.add').on('click', function(event) {
    event.preventDefault();
    $('.container').append('<p class="input" contenteditable="true"></p>');
});

$('a.enable').on('click', function(event) {
    event.preventDefault();
    $('.input').not('.showPopover').addClass('showPopover');
});

$('.container').on('focus', '.input.showPopover', function(event) {
    if (!$(this).data("bs.popover")) {                
        $(this).popover({
            placement:'right',
            trigger:'manual',
            html:true,
            content:'<a href="#" class="btn btn-danger">Remove</a>'
        });
    }
    $(this).popover('show');
});

var mousedownHappened = false;

$('.container').on('blur', '.input', function(event) {
    if(mousedownHappened) {
        mousedownHappened = false;
    } else {
        $(this).popover('hide');
    }
});

$('.container').on('mousedown', '.popover .btn', function(event) {
    mousedownHappened = true;
});

});

Jsfiddle:http://jsfiddle.net/Lh2rpj0f/2/

jQuery 1.11.1,引导 3.3.2


感谢 Yenne Info,我找到了一个可行的解决方案: http://jsfiddle.net/Lh2rpj0f/4/

这可能不是最好的解决方案,但它完全符合我的要求。 (当我单击弹出框内的按钮时,单击启用按钮时不会破坏此弹出框。)


至于现在,我的最终解决方案:Bootstrap popover with manual trigger attached on dynamic content

【问题讨论】:

    标签: javascript jquery twitter-bootstrap bootstrap-popover


    【解决方案1】:

    我更新了我的原始代码,现在它也可以正常工作了。

    $('.container').on('focus', '.input.showPopover', function(event) {
        if (!$(this).data("bs.popover") || !$(this).attr('data-popoverAttached')) {
            $(this).popover('destroy').popover({
                placement:'right',
                trigger:'manual',
                html:true,
                content:'<a href="#" class="btn btn-danger">Remove</a>'
            });
            $(this).attr('data-popoverAttached', true);
        }
        $(this).popover('show');
    });
    

    JSfiddle:http://jsfiddle.net/Lh2rpj0f/5/

    但是,我认为引导弹出框代码内部有问题。我认为我的原始代码不起作用的原因是引导弹出窗口以某种方式神奇地附加(使用默认选项!)到我所有动态添加的 div(即使它们没有类 .showPopover)。因此,当焦点触发时,它不会通过 if 语句。 data-popoverAttached 属性解决了这个问题。

    【讨论】:

      【解决方案2】:

      here 所述,您需要为每个元素动态生成工具提示。按照答案中给出的示例,将mouseentermouseleave 绑定到容器上,并在必要时创建新的工具提示。

      【讨论】:

      • 好吧,我的问题的标题可能并不准确。当我放弃 .showPopover 类时,它确实有效(只需尝试从 jsfiddle 中的焦点事件处理程序中删除 .showPopover)。所以,正如我的问题内容中所述:问题是,我只希望弹出框出现在具有特定类的 div 中,而不是与 div 一起添加。
      【解决方案3】:

      您可以重置和设置弹出框...

      小提琴http://jsfiddle.net/Lh2rpj0f/3/

      JS:

          jQuery(document).ready(function($) {
      
      $('a.add').on('click', function(event) {
          event.preventDefault();
          $('.container').append('<div class="input" contenteditable="true"></div>');
      });
      
      $('a.enable').on('click', function(event) {
          event.preventDefault();
          $('.input').not('.showPopover').addClass('showPopover');
          unset();set();
      });
      
          set();
      
          function unset(){
              $('.input').popover('destroy');
      
          }
      
      
          function set(){
              $('.container').on('focus', '.input.showPopover', function(event) {
                  if (!$(this).data("bs.popover")) {                
                      $(this).popover({
                          placement:'right',
                          trigger:'manual',
                          html:true,
                          content:'<a href="#" class="btn btn-danger">Remove</a>'
                      });
                  }
                  $(this).popover('show');
              });
              $('.container').on('blur', '.input', function(event) {
                  if(mousedownHappened) {
                      mousedownHappened = false;
                  } else {
                      $(this).popover('hide');
                  }
              });
      
              $('.container').on('mousedown', '.popover .btn', function(event) {
                  mousedownHappened = true;
              });
          }
      
      
      
      var mousedownHappened = false;
      
      
      });
      

      【讨论】:

      • 它确实有效,但是当我在其中一个 div 上打开 popover 时,它会破坏它。有没有更好的解决方案?
      • 这就是我有 mousedown 事件的原因。它发生在模糊之前,因此我可以单击弹出框内的按钮。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2012-10-07
      • 1970-01-01
      • 2023-03-15
      • 2018-12-27
      • 1970-01-01
      相关资源
      最近更新 更多