【问题标题】:Twitter Bootstrap: Hide other popovers when one is openTwitter Bootstrap:打开时隐藏其他弹出框
【发布时间】:2013-02-07 20:46:52
【问题描述】:

以下引导代码为我提供了“粘性”弹出框(因此用户可以与弹出框内的内容进行交互)。问题是当一个弹出窗口打开时,其他弹出窗口应该关闭(隐藏)。知道如何实现吗?

$("[rel=popover]").popover({placement:'bottom', trigger:'manual'}).hover(function(){
    $(this).popover('show');
    e.preventDefault(); 
});

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    这里有一个very simple solution(不是我的解决方案,但效果很好):

    $('.link-popover').click(function(){
        $('.link-popover').not(this).popover('hide'); //all but this
    });
    

    【讨论】:

    • 在不出现错误的情况下无法正常工作。对于其他寻找这样一个简单解决方案的人,我将以下代码添加到我的解决方案中的“显示弹出框事件”中,它只是关闭所有其他弹出框 $('.popover').not(this).hide();跨度>
    • 你成功了:),你可以缓存链接弹出框var $link = $('.link-popover'),然后使用缓存的变量$link.click ...
    • 演示链接已损坏。 .link-popover 是什么? -- 是触发链接还是弹出容器?
    【解决方案2】:

    根据引导文档:Use the focus trigger to dismiss popovers on the next click that the user makes.

    <a href="#" tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-   trigger="focus" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a>
    

    【讨论】:

    • 最简单有效的解决方案根本没有票?我的一份!
    • 仅适用于 Bootstrap 3。
    【解决方案3】:

    我一直在玩这个,还有一些关于触发手动显示/隐藏以使其正常播放的问题。悬停实际上是mouseinmouseout,除非你添加一些额外的检查,否则你会遇到我刚刚做的问题!

    Here is my solution in action 我会试着解释我做了什么。

    $(function () {
    
        var overPopup = false;
    
        $('[rel=popover]').popover({
            trigger: 'manual',
            placement: 'bottom'
    
        // replacing hover with mouseover and mouseout
        }).mouseover(function (e) {
            // when hovering over an element which has a popover, hide
            // them all except the current one being hovered upon
            $('[rel=popover]').not('#' + $(this).attr('id')).popover('hide');
            var $popover = $(this);
            $popover.popover('show');
    
            // set a flag when you move from button to popover
            // dirty but only way I could think of to prevent
            // closing the popover when you are navigate across
            // the white space between the two
            $popover.data('popover').tip().mouseenter(function () {
                overPopup = true;
            }).mouseleave(function () {
                overPopup = false;
                $popover.popover('hide');
            });
    
        }).mouseout(function (e) {
            // on mouse out of button, close the related popover
            // in 200 milliseconds if you're not hovering over the popover
            var $popover = $(this);
            setTimeout(function () {
                if (!overPopup) {
                    $popover.popover('hide');
                }
            }, 200);
        });
    });
    

    这对我来说非常适合以下 html:

    <a href="#" id="example1" class="btn large primary" rel="popover" data-content="Example 1!!!" data-original-title="Example 1 title">Button 1</a>
    <a href="#" id="example2" class="btn large primary" rel="popover" data-content="Example 2!!!" data-original-title="Example 2 title">Button 2</a>
    <a href="#" id="example3" class="btn large primary" rel="popover" data-content="Example 3!!!" data-original-title="Example 3 title">Button 3</a>
    

    希望能帮到你:)

    【讨论】:

    • 谢谢,这很好用。是否可以对此进行调整,以便当您将鼠标悬停在文档上其他任何地方的弹出框上时弹出框消失?现在在您的版本中,如果您将鼠标悬停在另一个项目上,弹出框就会消失,但如果您将鼠标移到页面上的其他任何位置,弹出框仍然保持打开状态。
    • 有点困惑...所以你想让它保持粘性直到你将鼠标悬停在弹出窗口中?
    • 我希望它在您在弹出框内交互的整个过程中保持粘性。但是,如果用户移动到例如页脚(不是另一个启用弹出框的项目),弹出框仍然保持打开/活动状态。这也发生在您自己的演示中,尝试将鼠标悬停在最后一项上,然后继续向右移动鼠标。你可以看到弹出框永远不会消失。
    • 我已经更新了我的答案,尽管我发现您的 cmets 令人困惑...您希望对解决方案进行调整,以便当您将鼠标移到文档上的任何其他位置时弹出框消失,但如果用户移动对于页脚,弹出框应保持打开/活动状态...(除非您将弹出框称为页脚..?)
    • $popover.data('popover') 从今天起无法使用。请改用$popover.data('bs.popover')
    【解决方案4】:

    使用Bootstrap 3's event callbacks 你可以做到:

    $(document).on('show.bs.popover', function() {
      $('.popover').not(this).popover('hide');
    });
    

    在咖啡脚本中

    $(document).on 'show.bs.popover', ->
        $('.popover').not(this).popover('hide')
    

    【讨论】:

    • 引入双击错误。您必须单击两次才能打开其他弹出窗口。
    【解决方案5】:

    关闭所有其他弹出窗口的 Simpiet 解决方案。这可以添加到任何会出现弹出窗口的事件中,例如单击/悬停等。就在您在以下代码中显示弹出窗口粘贴之前:

    $('.popover').not(this).hide(); //Hides all other popovers
    

    这将删除页面上除当前弹出框之外的所有弹出框

    【讨论】:

    • 如何将其转换为咖啡
    • 我从未使用过 CoffeeJS,但我使用了一个在线转换器js2coffee.org 将其输出为翻译? $('.popover').not(this).hide(); //隐藏所有其他弹出框
    • 效果很好,我用这个link更新了。
    【解决方案6】:
    $('li').popover({
        title: 'My title',
        content: 'My content'
    })
    .on('show.bs.popover', function() {
        if (window._bsPopover) {
            $(window._bsPopover).popover('hide')
        }
        window._bsPopover= this;
    })
    .on('hide.bs.popover', function() {
       window._bsPopover= null; // see Peter Jacoby's comment
    });
    

    【讨论】:

    • 为了避免弹窗关闭后无法再次打开的问题,可以添加这个来重置参数:.on('hide.bs.popover', function () { window.activePopover = null; })
    【解决方案7】:

    我为我的内容使用了一个函数,所以我有(在咖啡脚本中):

    provideContentForPopover = (element) ->
      $('.some-selector').not(element).popover 'hide'
      "some content to be returned"
    
    $('.some-selector').popover
      content: -> provideContentForPopover @
    

    【讨论】:

      【解决方案8】:

      我为我的内容使用了一个函数,它可以正常工作。

      $(function () {                     
          $('[data-toggle="popover"]').click(function(){
              $(this).popover('toggle');
              $('[data-toggle="popover"]').not(this).popover('hide'); //all but this
          });
      })
      

      【讨论】:

        【解决方案9】:
        $('.allThePopovers').click(function () {
            if ($(this).hasClass('popoverIsOpen')) {
                $(this).removeClass('popoverIsOpen');
            } else {
                $('.popoverIsOpen').popover('hide');
                $('.allThePopovers').removeClass('popoverIsOpen');
                $(this).addClass('popoverIsOpen');
        });
        

        只需将 click 替换为 hover 或 mousein 即可满足您的需求。

        【讨论】:

        • 否决者可以向我解释这个解决方案有什么问题吗?对本网站的建设性批评表示赞赏。
        【解决方案10】:

        如果您只想一次打开一个弹出框,通过单击打开和关闭(光标位置无关),这很好用:

        $('[data-toggle="popover"]').popover({  html: true }).bind("click", function(){ 
        
          if(!$(this).parent().children("a").first().is(":hover"))
           $( '[data-toggle="popover"]').popover("hide"); 
          else
           $( '[data-toggle="popover"]').not($(this).parent().children("a").first()).popover("hide"); 
        
         return false;  
        });
        

        重要的是每个弹出框都有一个单独的父级,例如

        <ul> <li> <popover> </li> <li> <popover> </li> </ul>
        

        HTML:

         <li>  
          <a id="quickmenu-i-305" data-toggle="popover" data-placement="bottom" data-title="Title" data-content='<h2>Plesk Login</h2>' href="Plesk Login">Ihr Kundenbereich</a> 
         </li>
        

        【讨论】:

          【解决方案11】:

          我可以通过隐藏不是被点击的弹出框来完成类似的操作。我不确定,但它似乎对我很有效。这是为了在单击时显示弹出框并保持活动状态。单击另一个弹出框时它会隐藏。

          <script>
          $(function () {
              $('[rel=popover]').popover({
              }).click(function (e) {
                  $('[rel=popover]').not('#friend_' + $(this).attr('id')).popover('hide');
              });
          });
          </script>
          

          【讨论】:

            【解决方案12】:

            更简单的方法来完成这项工作:

            $('[rel=popover]').popover({
                trigger: 'manual',
                placement: 'bottom'
            }).click(function(e) {
                $('[rel=popover]').not('#' + $(this).attr('id')).popover('hide');
                var $popover = $(this);
                $popover.popover('toggle');
            });
            

            只需确保您的弹出框具有唯一 ID ;] 默认情况下,您的弹出窗口的行为将与默认情况相同,一次只有一个弹出窗口。

            【讨论】:

              【解决方案13】:

              我发现动态弹出框有些问题,所以 这是静态和动态弹出框的两种解决方案:

              第一个解决方案是使用弹出选项triger:'focus' 但此选项不适用于某些 android 设备

              第二个:

              $('body').popover({
                  html: true,
                  //this is for static and dynamic popovers
                  selector: '[data-toggle="popover"]',
                  trigger: 'click',
                  content: function () {
                      //i am using predefined content for popovers. replace with your code or remove at all
                      return $($(this).data('templateselector') + ' .content').html();
                  },
                  title: function () {
                      return $($(this).data('templateselector') + ' .title').html();
                  },
                  container: 'body'
              }).on('show.bs.popover', function (e) {
                  // i've found that showed popovers has aria-describedby
                  // and $('[data-toggle="popover"]).not(this) not working for dynamic popovers so i came with this:
                  $('[data-toggle="popover"][aria-describedby]').popover('hide');
                  var trigger = $(e.target);
                  // this is for adding custom class for popover container
                  // just remove it if you dont need
                  trigger.data('bs.popover').tip().addClass($(trigger).data('class'));
              });
              

              【讨论】:

                【解决方案14】:

                为了兼容性,您必须使用&lt;a&gt; 锚标记。

                我的小提琴: https://jsfiddle.net/oneflame/pnb8Ltj3/

                引导程序Link -- http://getbootstrap.com/javascript/#dismiss-on-next-click

                <div id="FragmentText1">
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, est laborum.
                </div>
                
                $(document).ready(function(){               
                  $('a.LexicalEntry').popover({ 
                    html : true,    
                    content: function() {
                      return getLexicalDefinition($(this).text());
                    }   ,   
                    trigger: 'focus',
                    placement: 'auto',
                    html: true,
                    container: 'body'
                  });
                });
                
                // *** Injects HTML into raw text.
                // *** Splits the content string using a whitespace regular expression. 
                $('#FragmentText1').each(function() {
                
                    // var words = $.trim( $(this).text() ).split(/\s+/g);
                
                    var $this = $(this);
                    $this.html(
                        $this.text().trim().replace(/\b(\w+)\b/g,
                            "<a tabindex='0' class='LexicalEntry'' role='button' title='Definition: $1'>$1</a>"
                        ));
                
                });
                

                【讨论】:

                  【解决方案15】:

                  当您将鼠标悬停在或单击其他元素以打开弹出框时,使用此方法隐藏所有其他弹出框

                  一个 二 三 四个

                  $(document).ready(function(){    
                      $('.btnPopover').mouseover(function(){    
                      $(this).popover({
                          html: true,
                          trigger: 'manual'
                      }).popover('show');
                          $('.btnPopover').not(this).popover('hide');
                      }); 
                  });
                  

                  确保将 bootstrap.js 和 bootstrap.css 添加到您的页面。 希望这会有所帮助。

                  干杯!! 苏拉吉库马尔

                  【讨论】:

                  猜你喜欢
                  • 2012-09-02
                  • 1970-01-01
                  • 2018-01-01
                  • 1970-01-01
                  • 2013-01-05
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-01-22
                  相关资源
                  最近更新 更多