2014-01-20 79 views
1

我已经看看这个答案:Auto hide bootstrap popover,但它似乎并没有涵盖我的确切场景。隐藏Bootstrap弹出后延迟

我初始化我在窗口中加载功能popovers这样的:

$(window).load(function(){ 
    $('#fileArea').popover({ 
    selector: '[rel=popover]' 
    }); 
}); 

触发器是一个表行元素。通常当用户点击一行时,一个项目被添加到队列中。我没有绑定或解绑一堆东西,而是设置了两个独立的点击处理程序。一个处理不具有rel="popover"(和执行的代码后,我再加入rel属性!)和一个处理,确实有rel="popover"

表行表行这是所有工作的花花公子;单独的听众成功地区分了两种类型的点击。下面是显示上的元素与有效rel属性的弹出代码:

$('#fileArea').on('click', 'tbody [rel=popover]', function(e) { 
    var $this = $(this); 
    $this.popover({container: '#fileArea', placement: 'top auto', content:"This file has already been added to the queue."}); 
    setTimeout(function() {$this.popover('hide')},3000); 
}); 

因此,popovers已经“初始化”(有点......使用与#fileArea选择参数作为实际收听) ,然后在调用popover方法时,传入的参数将创建成功的popover。 setTimeout也适用!

但后来我发现,如果用户再次单击,弹出窗口不会出现。我怀疑它与传递参数有关,而不是仅仅调用void方法或传递字符串'show'。不幸的是,我需要传递参数,而不是使用数据属性来存储内容。

从控制台,如果我选择该行,然后调用$el.popover('show'),弹出窗口再次出现。

我目前的想法是,我需要弄清楚该行是否已经配置了一个弹出窗口(不只是初始化,但已配置)。但是,我不知道如何找到这个以创建一个条件。伪造的代码看起来是这样的:

$('#fileArea').on('click', 'tbody [rel=popover]', function(e) { 
    var $this = $(this); 
    if(/* popover is configured */) { 
    $this.popover('show'); 
    } else { 
    $this.popover({container: '#fileArea', placement: 'top auto', content:"This file has already been added to the queue."}); 
    } 
    setTimeout(function() {$this.popover('hide')},3000); 
}); 

任何人都有想法检测是否配置了弹出窗口?任何替代方式来实现我的目标?

回答

2

当你初始化酥料饼的设置元件上的.data()值:

$('#fileArea').on('click', 'tbody [rel=popover]', function(e) { 
    var $this = $(this); 
    if($this.data("popoverInitialized")) { 
    $this.popover('show'); 
    } else { 
    $this.popover({container: '#fileArea', placement: 'top auto', content:"This file has already been added to the queue."}); 
    $this.data("popoverInitialized", true); 
    } 
    setTimeout(function() {$this.popover('hide')},3000); 
}); 

或者的而不是躲在酥料饼,摧毁它:

$('#fileArea').on('click', 'tbody [rel=popover]', function(e) { 
    var $this = $(this); 
    $this.popover({container: '#fileArea', placement: 'top auto', content:"This file has already been added to the queue."}); 
    setTimeout(function() {$this.popover('destroy')},3000); 
}); 

或者,显示popover每次:

$('#fileArea').on('click', 'tbody [rel=popover]', function(e) { 
    var $this = $(this); 
    $this.popover({container: '#fileArea', placement: 'top auto', content:"This file has already been added to the queue."}); 
    $this.popover('show'); 
    setTimeout(function() {$this.popover('hide')},3000); 
}); 
+0

充满想法!我会试一试。谢谢一堆。 –

+0

我不知道为什么第一个解决方案无法正常工作,但是事实并非如此。然而,“摧毁”的想法很有效。我没有尝试第三种,因为我最喜欢前两种选择。谢谢! –