1

从这个jsFiddle可以看出,我有一个 Boostrap Popover,它可以通过外部点击以及点击“x”来关闭。

但是,当您单击另一个按钮弹出框时,第一个弹出框不会消失。

有没有办法更改 javascript,以便第一个弹出框在任何点击时消失?

HTML:

<form action="quote-calculator.php" method="post">

<div class="bs-docs-example" style="padding-bottom: 24px;">
  <a href="#" class="more-info btn btn-large btn-danger" data-toggle="popover" data-content="And here's some amazing content. It's very engaging. right?">Click to toggle popover</a>
</div>

<div class="bs-docs-example" style="padding-bottom: 24px;">
  <a href="#" class="more-info btn btn-large btn-danger" data-toggle="popover" data-content="And here's some amazing content. It's very engaging. right?">Click to toggle popover</a>
</div>

<div class="bs-docs-example" style="padding-bottom: 24px;">
  <a href="#" class="more-info btn btn-large btn-danger" data-toggle="popover" data-content="And here's some amazing content. It's very engaging. right?">Click to toggle popover</a>
</div>

</form>

jQuery:

    var isVisible = false;
    var clickedAway = false;

    $('.btn-danger').popover({
            html: true,
            trigger: 'manual'
        }).click(function(e) {
            $(this).popover('show');
        $('.popover-content').append('<button class="close" style="position: absolute; top: 0; right: 6px;">&times;</button>');
            clickedAway = false
            isVisible = true
            e.preventDefault()
        });

    $(document).click(function(e) {
      if(isVisible & clickedAway)
      {
        $('.btn-danger').popover('hide')
        isVisible = clickedAway = false
      }
      else
      {
        clickedAway = true
      }
    });
4

2 回答 2

1

添加

$('.popover').hide();

$(this).popover('show');

popover这会在显示当前元素之前隐藏所有类元素。

祝你好运!

于 2013-03-27T19:37:05.660 回答
1

尝试添加

$('.close:visible').trigger('click');

$(this).popover('show');

Updated Demo Fiddle

于 2013-03-27T14:20:20.223 回答