显示一个弹出窗口并隐藏其他弹出窗口

时间:2013-04-22 14:53:43

标签: twitter-bootstrap

我有几个按钮,我需要每个按钮 我想要这样:
当我的用户点击其中一个时,我希望其他人被隐藏。因此只显示一个弹出窗口 检查并帮我纠正这个例子plz:

var mycontent='<div class="btn-group"> <button class="btn">Left</button> <button class="btn">Middle</button> <button class="btn">Right</button> </div>'
$('.btn').popover({
    html: true,
    content:mycontent,
    trigger: 'manual'
}).click(function(e) {
    $(this).popover('toggle');
    e.stopPropagation();
});

$('html').click(function(e) {
     $('.btn').popover('hide');
});

我的HTML:

<ul>
    <li>
        <a href="#" class="btn" data-toggle="popover" data-placement="bottom" title="" >Popover</a>
    </li>
    <li>
       <a href="#" class="btn" data-toggle="popover" data-placement="bottom" title="" >Popover</a> 
    </li>
</ul>

jsfiddle example

添加类似代码的东西以某种方式解决了我的问题:

$('.btn').click(function(e) {
     $('.btn').popover('hide');
});

但是在每个按钮上单击两次就会出错

13 个答案:

答案 0 :(得分:143)

不知怎的,我为我的需要创造了一个例子。我用了这段代码:

$('.btn').popover();

$('.btn').on('click', function (e) {
    $('.btn').not(this).popover('hide');
});

检查demo here

并更正了之前的demo 我希望它会帮助别人

答案 1 :(得分:22)

我之前看到的答案都没有动态弹出窗口,所以这就是我想出来的。正如一些人所指出的那样,如果弹出窗口没有使用.remove()从DOM中删除,则会出现问题。我从引导程序网站分叉an example并创建了this new fiddle。使用selector: '[rel=popover]'选项添加动态弹出框。当即将显示弹出窗口时,我在所有其他弹出窗口上调用destroy,然后从页面中删除.popover内容。

$('body').popover({
                selector: '[rel=popover]',
                trigger: "click"
            }).on("show.bs.popover", function(e){
                // hide all other popovers
                $("[rel=popover]").not(e.target).popover("destroy");
                $(".popover").remove();                    
            });

答案 2 :(得分:20)

最简单的方法是在弹出框中设置convertView = inflater.inflate(R.layout.list_item_interaction, parent, false);

  

点击下一次点击

     

使用焦点触发器在下次单击时关闭弹出窗口   用户制作。

trigger="focus"

注意 - 这意味着弹出窗口会在您点击它时立即隐藏

答案 3 :(得分:10)

这是一个快速通用的解决方案,我使用的地方是您不需要事先知道类的弹出窗口。我还没有进行过广泛的测试。我也使用下面的切换,因为我遇到的一些问题与隐藏的行为完全不同于切换。

  var $currentPopover = null;
  $(document).on('shown.bs.popover', function (ev) {
    var $target = $(ev.target);
    if ($currentPopover && ($currentPopover.get(0) != $target.get(0))) {
      $currentPopover.popover('toggle');
    }
    $currentPopover = $target;
  });

  $(document).on('hidden.bs.popover', function (ev) {
    var $target = $(ev.target);
    if ($currentPopover && ($currentPopover.get(0) == $target.get(0))) {
      $currentPopover = null;
    }
  });

答案 4 :(得分:4)

你对此非常重视,只需关闭每个打开的popover,然后再触发新的popover:

startRecMode

答案 5 :(得分:4)

使用Bootstrap 3.3.7我找到了这个解决方案:

var _popoverLink = $('[data-toggle="popover"]');

_popoverLink.on('click', function(){
  _popoverLink.popover('destroy').popover({container: 'body'});
  $(this).popover('show');
});

问候。

答案 6 :(得分:2)

这是一个对我有用的解决方案。在我的脚本中,我没有通过HTML中的数据属性传递变量,我更喜欢我的js文件中的逻辑。

            $(".vote").popover({
                trigger: " click",
                title: "Attention",
                content: "You must be a member of the site to vote on answers.",
                placement: 'right'
            });

            $('.vote').on('click', function (e) {
                $('.vote').not(this).popover('hide');
            });

答案 7 :(得分:0)

我在使用bootstrap v3解决此问题时使用任何答案都遇到了一些困难。经过一些搜索,我发现我的主要问题是没有设置正确的弹出触发器。它应该设置为操作问题中列出的“手动”。

下一步非常简单,并且提供了比我在其他答案中看到的解决方案更好的行为,所以我想我会分享。

<subsystem xmlns="urn:jboss:domain:naming:1.1" >
    <bindings>
        <simple name="java:global/a.b.c" value="http://localhost:8080/proj.abc.app/ABCServiceEndpoint?wsdl" type="java.lang.String" />
    </bindings>
</subsystem>

此解决方案使您能够在单击页面上的任何其他位置(包括另一个弹出链接)时关闭弹出窗口,但允许您单击弹出窗口本身而不会忽略它,以便用户可以访问弹出窗口等复制内容粘贴文字。

希望这有助于某人,这是一个工作小提琴。 https://jsfiddle.net/hL0pvaty/

P.S。 - 我在我的例子中只使用.btn类作为选择器,因为它在op的问题中使用。我建议使用不太通用的东西。

答案 8 :(得分:0)

我选择了我在这个帖子和其他人看到过的方法组合。我的要求指明:

  • 一次只能看到一个弹出窗口
  • 点击popover外的任何地方都应该关闭popover
  • popover应包含控件
  • 不应要求重新绑定popover事件

        function bindEvents() {
    
             setupPopupBinding();
    
             setupPopupDismissal();
        };
    
        function setupPopupBinding() {
               $('.addSectionItem').popover({
                      html: true,
                      content: function () {
                            return createDropdowns($(this).data('sectionid'))[0].outerHTML;
                      },
                      placement: "right",
                      trigger: "click focus"
               }).on("inserted.bs.popover", function (e) {
                    //initialize dropdown
                    setupSelect2();
               }).on("hide.bs.popover", function (e) {
                     $('.select2-container--open').remove();
               });
        }
    
        function setupPopupDismissal() {
               $('body:not(.addSectionItem)').on('click', function (e) {
               $('.addSectionItem').each(function () {
               //the 'is' for buttons that trigger popups
               //the 'has' for icons within a button that triggers a popup
               if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
               $(this).popover('hide');
               $('.popover').has(e.target).remove();
            }
        });
        });
        }
    
        function createDropdowns(sectionId: number) {
               var dropdown = $('<div/>')
               .attr('Id', 'sectionPopupWrapper' + sectionId)
               .addClass('popupWrapper')
               .append($('<select/>').addClass('sectionPopup'))
               .append($('<button/>').addClass('btn btn-primary btn-xs')
               .attr('data-sectionid', sectionId)
               .text('Add'));
    
        return dropdown;
        }
    

答案 9 :(得分:0)

<ul>
<li><i class="fa fa-trash-o DeleteRow" id=""></i>1</li>
<li><i class="fa fa-trash-o DeleteRow" id=""></i>2</li>
<li><i class="fa fa-trash-o DeleteRow" id=""></i>3</li>
</ul>

// Close other popover when click on Delete/Open new popover - START //
     $('.DeleteRow').on('click', function (e) {
        $('.popover').not(this).popover('hide');
     });
 // Close other popover when click on Delete/Open new popover - END//

答案 10 :(得分:0)

借助“ losmescaleros”答案,这对我来说非常合适:

$('body').popover({
  selector: '[data-toggle="popover"]',
  trigger: "click"
}).on("show.bs.popover", function(e){
  // hide all other popovers
  $("[data-toggle='popover']").not(e.target).popover("destroy");                  
});

没有任何双击问题。

答案 11 :(得分:0)

这是最简单,最优雅的方法:

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

答案 12 :(得分:0)

点击图标并打开相应的弹出框后,它的值便以“ popover *”开头,称为aria- describeby。

因此,您找到了这些图标并触发了对它们的单击,但没有触发现在单击的图标。

$('.icon-info').click(function(){
    $(".icon-info[aria-describedby*='popover']").not(this).trigger('click');
});
相关问题