Bootstrap 3 popover show one and hide others doesn't work

倾然丶 夕夏残阳落幕 提交于 2019-12-11 12:39:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!