问题
HTML
<ul class="navbar-nav pull-right">
<li><a href="#" data-toggle="popover" title="Notifiche" data-html="true" data-content="Notification1<hr />Notification 2<hr />Notification 3">Notifications</a></li>
<li><a href="#" data-toggle="popover" title="Messages" data-html="true" data-content="Message 1<hr />Message 2<hr />Message 3">Messages</a></li>
</ul>
JAVASCRIPT
$('[data-toggle="popover"]').popover({placement: 'bottom', trigger: 'click' });
//hide popover when click outside
$('body').on('click', function (e) {
$('[data-toggle="popover"]').each(function () {
if ($(this).has(e.target).length === 0 && $('.popoVer').has(e.target).length === 0) {
$(this).popover('hide');
}
});
});
CSS
.popover-content {
max-height: 150px;
overflow-y: scroll;
}
I have these two popovers in my page, and when i click in one of this, and after i click in the second, the first popover hides, and everything works fine. But when i after click again on the first popover, it seems doesn't work properly. The mouse can not click in the scroll, and the links in the popover don't work fine. I think the browser is holding open the last opened popover, even if it is hide. Any suggestion? Thanks!
回答1:
I solved (thanks to another post on popovers) my problem adding this code:
// hide other popovers (and their links from the DOM) opened
$(document).mouseup(function (e) {
if ($('.popover').has(e.target).length === 0) {
$('.popover').toggleClass('in').remove();
return;
}
});
来源:https://stackoverflow.com/questions/21443284/bootstrap-3-popover-show-one-and-hide-others-doesnt-work