【问题标题】:Can I get the current state of a twitter bootstrap popover?我可以获取 twitter 引导弹出框的当前状态吗?
【发布时间】:2012-11-01 19:45:28
【问题描述】:

我目前调用一个脚本来动态地将内容添加到我的弹出框,但是当用户再次单击以关闭它时,我不需要进行此调用。是否可以获取状态并在可见时关闭它?

这是我目前所拥有的:

$('.knownissue').on('click', function() {

    var info = $(this).attr('id').substr(5).split(':');
    var el = $(this);

    // How do I check to see if the popover is visible
    // so I can simply close it and exit?

    $.post('functions/get_known_issues.php?tcid='+info[0]+'&tcdate=' + info[1], function(data) {
        if (data.st) {
            el.attr('data-content', data.issue);
            el.popover('toggle');
        }
    }, "json");

});

【问题讨论】:

标签: javascript jquery twitter-bootstrap


【解决方案1】:

为避免搜索动态插入的标记,您可以这样做:

在引导程序 2 中:

var $element = $('.element-you-added-popup-to')
$element.data().popover.tip().hasClass('in')
// Or if you used '.tooltip()' instead of '.popover()'
$element.data().tooltip.tip().hasClass('in')

在引导程序 3 中:

$element.data()['bs.popover'].tip().hasClass('in')
$element.data()['bs.tooltip'].tip().hasClass('in')

【讨论】:

  • Caused Uncaught TypeError: Cannot read property 'popover' of undefined in Chrome 45. 通过 $element.data()['bs.popover'].tip().hasClass('in') 修复
【解决方案2】:
if($('.popover').hasClass('in')){
  // popover is visable
} else {
  // popover is not visable
}

【讨论】:

  • 你可以有很多弹窗,如果显示其他弹窗就会失败。
【解决方案3】:

默认情况下,隐藏和显示的事件在引导弹出窗口中可用。

$('#myPopover').on('shown.bs.popover', function () {
  // do something…
})

以下用于引导弹出窗口的事件可用

show.bs.popover	
shown.bs.popover
hide.bs.popover	
hidden.bs.popover

更多详情请参考文档bootstrap popovers

【讨论】:

    【解决方案4】:

    对于 Bootstrap 4.x

     $('a#aUeberschriftenJBi').click(function() {
        if ($('a#aUeberschriftenJBi').attr('aria-describedby')) {
            // popover is visable
        } else {
            // popover is not visable
            $('a#aUeberschriftenJBi').popover({
                placement: 'bottom',
                title: function() {
                    return "Title";
                },
                content: function() {
                    return "Content";
                }
            });
        }
    
        $('a#aUeberschriftenJBi').popover("show");
    });
    

    HTML

    <a href="#" id="aUeberschriftenJBi" title=""><span><i class="fa fa-sticky-note"></i></span></a>
    

    【讨论】:

      【解决方案5】:

      Bootstrap 动态添加和删除弹出框的标记,因此您只需要检查元素是否存在。

      如果您转到 Bootstrap 示例页面:http://twitter.github.com/bootstrap/javascript.html#popovers,您可以使用显示“单击以切换弹出窗口”的红色大按钮来切换他们的示例弹出窗口

      这个 jquery 选择器被写来选择那个 popover 元素(如果它存在的话) $('#popovers').find('h3').eq(5).next().find('.popover')

      要检查它的状态(存在与否),请检查返回的元素集的长度是否为0。

      所以按下按钮来切换他们的弹出框示例,然后在控制台中运行以下命令:

      打开弹出窗口

      console.log( 
           $('#popovers').find('h3').eq(5).next().find('.popover').length === 0
      ); // false
      

      关闭弹出窗口

      console.log( 
          $('#popovers').find('h3').eq(5).next().find('.popover').length === 0
      ); // true
      

      PS - 希望你能写出更好的选择器,因为你已经知道你需要检查页面上是否存在弹出框

      编辑:link to jsfiddle HERE

      【讨论】:

      • 我想我不知道它会如何工作。我检查元素是否存在,它为两种切换状态返回 0...
      • 转到此处并单击弹出窗口:jsfiddle.net/eLjQG/7 我切换了条件以检查长度是否不相等,因此它在打开时会提示 true,否则会提示 false。
      【解决方案6】:

      试试这个

      if ($('#elementId').attr('aria-describedby')) {
        alert('Popover is Visible');
        $("#elementId").popover('hide'); // To Hide Popover
      
      }else{
        alert('Popover is Hidden');
        $("#elementId").popover('show'); // To Display Popover
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-11
        • 2018-08-22
        • 2021-09-01
        • 1970-01-01
        • 2012-08-19
        • 2013-03-28
        • 1970-01-01
        • 2013-05-31
        • 2023-03-17
        相关资源
        最近更新 更多