3

How to dismiss Bootstrap popover by clicking outside the popover. Currently it has toggle for open link.

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>

Here is js code

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

2 回答 2

6
$('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/

于 2013-10-03T11:31:53.187 回答
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');
        }
    }); 
});

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

于 2014-04-16T21:14:59.470 回答