我目前在Twitter Bootstrap中使用弹出窗口,其启动方式如下:
$('.popup-marker').popover({
html: true,
trigger: 'manual'
}).click(function(e) {
$(this).popover('toggle');
e.preventDefault();
});
如您所见,它们是手动触发的,单击.popup-marker(具有背景图像的div)可切换弹出框。这很好用,但是我也希望能够通过单击页面上其他任何位置(而不是弹出窗口本身)来关闭弹出窗口。
我尝试了几种不同的方法,包括以下内容,但没有结果可显示:
$('body').click(function(e) {
$('.popup-marker').popover('hide');
});
如何单击页面上的其他任何位置来关闭弹出窗口,而不能单击弹出窗口本身呢?
假设任何时候都只能看到一个弹出窗口,则可以使用一组标志来标记何时有可见的弹出窗口,然后再隐藏它们。
如果在文档正文上设置了事件监听器,则在单击标有“ popup-marker”的元素时将触发该事件监听器。因此,您必须调用stopPropagation()
事件对象。并在单击弹出框本身时应用相同的技巧。
以下是执行此操作的有效JavaScript代码。它使用jQuery> = 1.7
jQuery(function() {
var isVisible = false;
var hideAllPopovers = function() {
$('.popup-marker').each(function() {
$(this).popover('hide');
});
};
$('.popup-marker').popover({
html: true,
trigger: 'manual'
}).on('click', function(e) {
// if any other popovers are visible, hide them
if(isVisible) {
hideAllPopovers();
}
$(this).popover('show');
// handle clicking on the popover itself
$('.popover').off('click').on('click', function(e) {
e.stopPropagation(); // prevent event for bubbling up => will not get caught with document.onclick
});
isVisible = true;
e.stopPropagation();
});
$(document).on('click', function(e) {
hideAllPopovers();
isVisible = false;
});
});
http://jsfiddle.net/AFffL/539/
唯一的警告是您将无法同时打开2个弹出窗口。但是我认为这反而会使用户感到困惑:-)
这甚至更容易:
$('html').click(function(e) {
$('.popup-marker').popover('hide');
});
$('.popup-marker').popover({
html: true,
trigger: 'manual'
}).click(function(e) {
$(this).popover('toggle');
e.stopPropagation();
});
我也有类似的需求,并发现Lee Carmichael的Twitter Bootstrap Popover的这个小扩展名叫做BootstrapX-clickover。他在这里也有一些用法示例。基本上,它将弹出窗口更改为交互式组件,当您单击页面上的其他位置或弹出窗口中的关闭按钮时,该组件将关闭。这也将允许一次打开多个弹出窗口以及许多其他不错的功能。
使用范例
<button rel="clickover" data-content="Show something here.
<button data-dismiss='clickover'
>Close Clickover</button>"
>Show clickover</button>
javascript:
// load click overs using 'rel' attribute
$('[rel="clickover"]').clickover();
可接受的解决方案给了我一些问题(单击已打开的弹出窗口的'.popup-marker'元素会使弹出窗口在以后无法正常工作)。我想出了另一种最适合我的解决方案,它很简单(我使用的是Bootstrap 2.3.1):
$('.popup-marker').popover({
html: true,
trigger: 'manual'
}).click(function(e) {
$('.popup-marker').not(this).popover('hide');
$(this).popover('toggle');
});
$(document).click(function(e) {
if (!$(e.target).is('.popup-marker, .popover-title, .popover-content')) {
$('.popup-marker').popover('hide');
}
});
更新:此代码也适用于Bootstrap 3!
在http://getbootstrap.com/javascript/#popovers上阅读“在下次点击时关闭”
您可以使用焦点触发器在下次点击时消除弹出式窗口,但是您必须使用<a>
标签而不是<button>
标签,并且还必须包含一个tabindex
属性...
例:
<a href="#" tabindex="0" class="btn btn-lg btn-danger"
data-toggle="popover" data-trigger="focus" title="Dismissible popover"
data-content="And here's some amazing content. It's very engaging. Right?">
Dismissible popover
</a>
现有的所有答案都相当薄弱,因为它们依赖于捕获所有文档事件,然后查找活动的弹出窗口,或修改对的调用.popover()
。
更好的方法是侦听show.bs.popover
文档正文中的事件,然后做出相应的反应。下面的代码将在单击或esc按下文档时关闭弹出窗口,仅在显示弹出窗口时绑定事件监听器:
function closePopoversOnDocumentEvents() {
var visiblePopovers = [];
var $body = $("body");
function hideVisiblePopovers() {
$.each(visiblePopovers, function() {
$(this).popover("hide");
});
}
function onBodyClick(event) {
if (event.isDefaultPrevented())
return;
var $target = $(event.target);
if ($target.data("bs.popover"))
return;
if ($target.parents(".popover").length)
return;
hideVisiblePopovers();
}
function onBodyKeyup(event) {
if (event.isDefaultPrevented())
return;
if (event.keyCode != 27) // esc
return;
hideVisiblePopovers();
event.preventDefault();
}
function onPopoverShow(event) {
if (!visiblePopovers.length) {
$body.on("click", onBodyClick);
$body.on("keyup", onBodyKeyup);
}
visiblePopovers.push(event.target);
}
function onPopoverHide(event) {
var target = event.target;
var index = visiblePopovers.indexOf(target);
if (index > -1) {
visiblePopovers.splice(index, 1);
}
if (visiblePopovers.length == 0) {
$body.off("click", onBodyClick);
$body.off("keyup", onBodyKeyup);
}
}
$body.on("show.bs.popover", onPopoverShow);
$body.on("hide.bs.popover", onPopoverHide);
}
https://github.com/lecar-red/bootstrapx-clickover
它是twitter bootstrap popover的扩展,将非常简单地解决问题。
由于某种原因,这里没有其他解决方案对我有用。但是,经过大量的故障排除后,我终于找到了这种方法(至少对我而言是完美的)。
$('html').click(function(e) {
if( !$(e.target).parents().hasClass('popover') ) {
$('#popover_parent').popover('destroy');
}
});
在我的情况下,我在表中添加了一个弹出框,并将其绝对定位在td
被单击的上方/下方。表的选择是由jQuery-UI Selectable处理的,所以我不确定这是否会造成干扰。但是,每当我在弹出窗口内单击时,定位到我的点击处理程序就$('.popover')
永远不会起作用,并且事件处理总是委派给$(html)
单击处理程序。我是JS的新手,所以也许我只是想念一些东西?
无论如何,我希望这对某人有帮助!
我给所有的弹窗主播上课activate_popover
。我一次加载全部激活它们
$('body').popover({selector: '.activate-popover', html : true, container: 'body'})
为了获得我使用的点击功能(在咖啡脚本中):
$(document).on('click', (e) ->
clickedOnActivate = ($(e.target).parents().hasClass("activate-popover") || $(e.target).hasClass("activate-popover"))
clickedAway = !($(e.target).parents().hasClass("popover") || $(e.target).hasClass("popover"))
if clickedAway && !clickedOnActivate
$(".popover.in").prev().popover('hide')
if clickedOnActivate
$(".popover.in").prev().each () ->
if !$(this).is($(e.target).closest('.activate-popover'))
$(this).popover('hide')
)
这与bootstrap 2.3.1完美配合
即使这里有很多解决方案,我也想提出我的建议,我不知道那里是否有解决所有问题的解决方案,但是我尝试了其中的3个,但他们遇到了问题,例如单击在弹出式窗口上,它会自行隐藏,其他情况是,如果我单击另一个弹出式按钮,则同时单击两个/多个弹出式窗口仍会出现(如在选定的解决方案中一样),但是,此问题已解决。
var curr_popover_btn = null;
// Hide popovers function
function hide_popovers(e)
{
var container = $(".popover.in");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
if( curr_popover_btn != null )
{
$(curr_popover_btn).popover('hide');
curr_popover_btn = null;
}
container.hide();
}
}
// Hide popovers when out of focus
$('html').click(function(e) {
hide_popovers(e);
});
$('.popover-marker').popover({
trigger: 'manual'
}).click(function(e) {
hide_popovers(e);
var $popover_btns = $('.popover-marker');
curr_popover_btn = this;
var $other_popover_btns = jQuery.grep($($popover_btns), function(popover_btn){
return ( popover_btn !== curr_popover_btn );
});
$($other_popover_btns).popover('hide');
$(this).popover('toggle');
e.stopPropagation();
});
我将焦点设置为新创建的弹出窗口,并在模糊时将其删除。这样,就无需检查DOM的哪个元素,也可以单击并选择弹出窗口:它不会失去焦点,也不会消失。
编码:
$('.popup-marker').popover({
html: true,
trigger: 'manual'
}).click(function(e) {
$(this).popover('toggle');
// set the focus on the popover itself
jQuery(".popover").attr("tabindex",-1).focus();
e.preventDefault();
});
// live event, will delete the popover by clicking any part of the page
$('body').on('blur','.popover',function(){
$('.popup-marker').popover('hide');
});
这是对我来说很好的解决方案,如果可以帮助的话:
/**
* Add the equals method to the jquery objects
*/
$.fn.equals = function(compareTo) {
if (!compareTo || this.length !== compareTo.length) {
return false;
}
for (var i = 0; i < this.length; ++i) {
if (this[i] !== compareTo[i]) {
return false;
}
}
return true;
};
/**
* Activate popover message for all concerned fields
*/
var popoverOpened = null;
$(function() {
$('span.btn').popover();
$('span.btn').unbind("click");
$('span.btn').bind("click", function(e) {
e.stopPropagation();
if($(this).equals(popoverOpened)) return;
if(popoverOpened !== null) {
popoverOpened.popover("hide");
}
$(this).popover('show');
popoverOpened = $(this);
e.preventDefault();
});
$(document).click(function(e) {
if(popoverOpened !== null) {
popoverOpened.popover("hide");
popoverOpened = null;
}
});
});
这是我的解决方案,物有所值:
// Listen for clicks or touches on the page
$("html").on("click.popover.data-api touchend.popover.data-api", function(e) {
// Loop through each popover on the page
$("[data-toggle=popover]").each(function() {
// Hide this popover if it's visible and if the user clicked outside of it
if ($(this).next('div.popover:visible').length && $(".popover").has(e.target).length === 0) {
$(this).popover("hide");
}
});
});
我在启动引导程序2.3.2时遇到一些问题。但是我这样喜欢它:
$(function () {
$(document).mouseup(function (e) {
if(($('.popover').length > 0) && !$(e.target).hasClass('popInfo')) {
$('.popover').each(function(){
$(this).prev('.popInfo').popover('hide');
});
}
});
$('.popInfo').popover({
trigger: 'click',
html: true
});
});
略微调整了@David Wolever解决方案:
function closePopoversOnDocumentEvents() {
var visiblePopovers = [];
var $body = $("body");
function hideVisiblePopovers() {
/* this was giving problems and had a bit of overhead
$.each(visiblePopovers, function() {
$(this).popover("hide");
});
*/
while (visiblePopovers.length !== 0) {
$(visiblePopovers.pop()).popover("hide");
}
}
function onBodyClick(event) {
if (event.isDefaultPrevented())
return;
var $target = $(event.target);
if ($target.data("bs.popover"))
return;
if ($target.parents(".popover").length)
return;
hideVisiblePopovers();
}
function onBodyKeyup(event) {
if (event.isDefaultPrevented())
return;
if (event.keyCode != 27) // esc
return;
hideVisiblePopovers();
event.preventDefault();
}
function onPopoverShow(event) {
if (!visiblePopovers.length) {
$body.on("click", onBodyClick);
$body.on("keyup", onBodyKeyup);
}
visiblePopovers.push(event.target);
}
function onPopoverHide(event) {
var target = event.target;
var index = visiblePopovers.indexOf(target);
if (index > -1) {
visiblePopovers.splice(index, 1);
}
if (visiblePopovers.length == 0) {
$body.off("click", onBodyClick);
$body.off("keyup", onBodyKeyup);
}
}
$body.on("show.bs.popover", onPopoverShow);
$body.on("hide.bs.popover", onPopoverHide);
}
这里也有人问这个问题,我的回答不仅提供了一种了解jQuery DOM遍历方法的方法,还提供了两种通过单击外部来处理弹出窗口关闭的选项。
一次打开多个弹出窗口或一次打开一个弹出窗口。
另外,这些小代码段可以处理包含图标的按钮的关闭!
https://stackoverflow.com/a/14857326/1060487
这个作品就像一种魅力,我用它。
单击时,它将打开弹出窗口,如果再次单击,它将关闭,如果单击弹出窗口之外,也会关闭弹出窗口。
这也适用于1个以上的弹出框。
function hideAllPopovers(){
$('[data-toggle="popover"]').each(function() {
if ($(this).data("showing") == "true"){
$(this).data("showing", "false");
$(this).popover('hide');
}
});
}
$('[data-toggle="popover"]').each(function() {
$(this).popover({
html: true,
trigger: 'manual'
}).click(function(e) {
if ($(this).data("showing") != "true"){
hideAllPopovers();
$(this).data("showing", "true");
$(this).popover('show');
}else{
hideAllPopovers();
}
e.stopPropagation();
});
});
$(document).click(function(e) {
hideAllPopovers();
});
另一个解决方案,它解决了我点击弹出窗口后代的问题:
$(document).mouseup(function (e) {
// The target is not popover or popover descendants
if (!$(".popover").is(e.target) && 0 === $(".popover").has(e.target).length) {
$("[data-toggle=popover]").popover('hide');
}
});
我做如下
$("a[rel=popover]").click(function(event){
if(event.which == 1)
{
$thisPopOver = $(this);
$thisPopOver.popover('toggle');
$thisPopOver.parent("li").click(function(event){
event.stopPropagation();
$("html").click(function(){
$thisPopOver.popover('hide');
});
});
}
});
希望这可以帮助!
如果您尝试在pjax中使用twitter bootstrap popover,则对我有用:
App.Utils.Popover = {
enableAll: function() {
$('.pk-popover').popover(
{
trigger: 'click',
html : true,
container: 'body',
placement: 'right',
}
);
},
bindDocumentClickEvent: function(documentObj) {
$(documentObj).click(function(event) {
if( !$(event.target).hasClass('pk-popover') ) {
$('.pk-popover').popover('hide');
}
});
}
};
$(document).on('ready pjax:end', function() {
App.Utils.Popover.enableAll();
App.Utils.Popover.bindDocumentClickEvent(this);
});
@RayOnAir,我与以前的解决方案有相同的问题。我也接近@RayOnAir解决方案。改进的一件事是,当单击其他弹出标记时,关闭已打开的弹出窗口。所以我的代码是:
var clicked_popover_marker = null;
var popover_marker = '#pricing i';
$(popover_marker).popover({
html: true,
trigger: 'manual'
}).click(function (e) {
clicked_popover_marker = this;
$(popover_marker).not(clicked_popover_marker).popover('hide');
$(clicked_popover_marker).popover('toggle');
});
$(document).click(function (e) {
if (e.target != clicked_popover_marker) {
$(popover_marker).popover('hide');
clicked_popover_marker = null;
}
});
我发现这是上面pbaron的建议的修改后的解决方案,因为他的解决方案激活了所有带有“ popup-marker”类的元素的popover('hide')。但是,当您对HTML内容而不是数据内容使用popover()时(如我在下面所做的那样),该html弹出窗口中的任何单击实际上都会激活popover('hide'),这会立即关闭窗口。下面的此方法遍历每个.popup-marker元素,并首先发现父级是否与单击的.popup-marker的ID有关,如果是,则不隐藏它。其他所有div都被隐藏了...
$(function(){
$('html').click(function(e) {
// this is my departure from pbaron's code above
// $('.popup-marker').popover('hide');
$('.popup-marker').each(function() {
if ($(e.target).parents().children('.popup-marker').attr('id')!=($(this).attr('id'))) {
$(this).popover('hide');
}
});
});
$('.popup-marker').popover({
html: true,
// this is where I'm setting the html for content from a nearby hidden div with id="html-"+clicked_div_id
content: function() { return $('#html-'+$(this).attr('id')).html(); },
trigger: 'manual'
}).click(function(e) {
$(this).popover('toggle');
e.stopPropagation();
});
});
我想出了这个:
我的方案在同一页面上包含更多弹出窗口,并且将它们隐藏起来只是使其不可见,因此,无法单击弹出窗口后面的项目。这个想法是将特定的popover链接标记为“活动”,然后您可以简单地“切换”活动的popover。这样做将完全关闭弹出窗口。
$('.popover-link').popover({ html : true, container: 'body' })
$('.popover-link').popover().on 'shown.bs.popover', ->
$(this).addClass('toggled')
$('.popover-link').popover().on 'hidden.bs.popover', ->
$(this).removeClass('toggled')
$("body").on "click", (e) ->
$openedPopoverLink = $(".popover-link.toggled")
if $openedPopoverLink.has(e.target).length == 0
$openedPopoverLink.popover "toggle"
$openedPopoverLink.removeClass "toggled"
我试图为一个简单的问题提供一个简单的解决方案。以上职位很好,但是对于一个简单的问题来说太复杂了。所以我做了一件简单的事情。刚刚添加了一个关闭按钮。对我来说很完美。
$(".popover-link").click(function(){
$(".mypopover").hide();
$(this).parent().find(".mypopover").show();
})
$('.close').click(function(){
$(this).parents('.mypopover').css('display','none');
});
<div class="popover-content">
<i class="fa fa-times close"></i>
<h3 class="popover-title">Title here</h3>
your other content here
</div>
.popover-content {
position:relative;
}
.close {
position:absolute;
color:#CCC;
right:5px;
top:5px;
cursor:pointer;
}
我喜欢这个,简单却有效。
var openPopup;
$('[data-toggle="popover"]').on('click',function(){
if(openPopup){
$(openPopup).popover('hide');
}
openPopup=this;
});
将btn-popover
类添加到您的用于打开弹出窗口的弹出按钮/链接。当在其外部单击时,此代码将关闭弹出窗口。
$('body').on('click', function(event) {
if (!$(event.target).closest('.btn-popover, .popover').length) {
$('.popover').popover('hide');
}
});
一个甚至更简单的解决方案,只需遍历所有弹出窗口,然后隐藏即可this
。
$(document).on('click', '.popup-marker', function() {
$(this).popover('toggle')
})
$(document).bind('click touchstart', function(e) {
var target = $(e.target)[0];
$('.popup-marker').each(function () {
// hide any open popovers except for the one we've clicked
if (!$(this).is(target)) {
$(this).popover('hide');
}
});
});
$('.popForm').popover();
$('.conteneurPopForm').on("click",".fermePopover",function(){
$(".popForm").trigger("click");
});
要明确的是,只需触发弹出窗口
这应该在Bootstrap 4中工作:
$("#my-popover-trigger").popover({
template: '<div class="popover my-popover-content" role="tooltip"><div class="arrow"></div><div class="popover-body"></div></div>',
trigger: "manual"
})
$(document).click(function(e) {
if ($(e.target).closest($("#my-popover-trigger")).length > 0) {
$("#my-popover-trigger").popover("toggle")
} else if (!$(e.target).closest($(".my-popover-content")).length > 0) {
$("#my-popover-trigger").popover("hide")
}
})
说明:
- 第一部分根据文档启动弹出窗口:https : //getbootstrap.com/docs/4.0/components/popovers/
- 第二部分中的第一个“ if”检查被单击的元素是否是#my-popover-trigger的后代。如果是这样,它将切换弹出窗口(它处理触发器上的单击)。
- 第二部分中的第二个“ if”检查被单击的元素是否是init模板中定义的popover内容类的后代。如果不是,则表示单击不在弹出窗口内容之内,并且可以隐藏弹出窗口。
尝试data-trigger="focus"
代替"click"
。
这为我解决了问题。
文章标签:javascript , jquery , twitter-bootstrap
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!