单击外部,关闭Bootstrap popover

时间:2013-10-03 11:29:42

标签: javascript jquery twitter-bootstrap popover

如何通过单击popover外部来解除Bootstrap popover。目前它已切换为开放链接。

HTML

 <div class="widget-rating">
          <span class="rateit rating-average"></span>
          <a class="btn btn-mini" href="javascript:void(0)"><b class="caret"></b></a>
 </div>

这是js代码

element.popoverAnchor.popover({
        title: "Rating",
        animation: false,
        html: true,
        content: "Loading...",
        placement: "bottom",
        trigger: "click"
      });

2 个答案:

答案 0 :(得分:5)

$('body').on('click', function (e) {
    $('.popover-link').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');
        }
    });
});

参考:http://mattlockyer.com/2013/04/08/close-a-twitter-bootstrap-popover-when-clicking-outside/

答案 1 :(得分:0)

$('body').on('click', function (e) { 
    $("div.popover").each(function() {
        if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
            $(this).prevAll('*:first').popover('hide');
        }
    }); 
});

任何情况下的通用修改..无需为触发元素指定类