最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dismiss Bootstrap popover by clicking outside - Stack Overflow

programmeradmin4浏览0评论

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"
      });

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"
      });
Share Improve this question edited Feb 17, 2014 at 20:16 Jack Bonneman 1,80518 silver badges24 bronze badges asked Oct 3, 2013 at 11:29 SowmyaSowmya 27k21 gold badges101 silver badges137 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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');
        }
    });
});

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

$('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');
        }
    }); 
});

universal modification for any case.. no need to specify class for trigger element

发布评论

评论列表(0)

  1. 暂无评论