任何点击关闭Bootstrap Popover

时间:2013-03-27 14:12:18

标签: javascript jquery html css twitter-bootstrap

从这个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
      }
    });

2 个答案:

答案 0 :(得分:1)

尝试添加

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

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

Updated Demo Fiddle

答案 1 :(得分:1)

添加

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

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

这会隐藏所有popover类元素,然后再显示当前元素。

祝你好运!