jQuery:如何在外部点击时关闭popover?

时间:2015-10-26 07:34:33

标签: jquery twitter-bootstrap

我正在使用Krajee Bootstrap Popover X。它工作得很好,除了当我在弹出窗口外点击时它不会关闭。它仅在我单击触发弹出窗口或其他弹出按钮的按钮时关闭。我尝试了这里的例子How to dismiss a Twitter Bootstrap popover by clicking outside?,但它不起作用

1 个答案:

答案 0 :(得分:0)

这是我自己问题的解决方案。

var idpop = []; //Creating an array to store the id
//Since that when you create a popover using Krajee Popoover X plugin,
//you always add id's to it.
//By default, only one popover will displayed at a time.
$('.popover').on('shown.bs.modal', function() {
  //Get the id of the popover that is currently displayed
  idpop.push($(this).attr('id')); //Add the id to the Array
});
$('.popover').on('hidden.bs.modal', function() {
  //Empty the array when the popover is dismissed
  idpop = [];
});
$('html').on('click', function(e) {
  //when click outside the popover then dismiss the popover
  if ($(e.target).closest('.popover').length == 0) {
    $('#' + idpop[0]).popoverX('hide');
  }
});