Bootstrap popover destroy&每隔一次重新创作一次 [英] Bootstrap popover destroy & recreate works only every second time

查看:97
本文介绍了Bootstrap popover destroy&每隔一次重新创作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式销毁&重新创建一个特定的Bootstrap popover。所以我所做的是:

I want to programmatically destroy & recreate a specific Bootstrap popover. So what I do is:

$('#popoverspan').popover('destroy');
$('#popoverspan').popover({placement : 'bottom', trigger : 'hover', content : 'Here it is!'});

它只能每隔一秒工作一次。我认为这是破坏弹出窗口所需时间的问题,但即使在两条线之间增加延迟也无济于事。我在JSFiddle中重新创建了这个问题: http://jsfiddle.net/Lfp9ssd0/10/

And it works every second time only. I thought that it's the matter of the time it takes to destroy the popover, but even adding a delay between the two lines doesn't help. I recreated the problem in JSFiddle: http://jsfiddle.net/Lfp9ssd0/10/

为什么会这样?有人建议它有效,例如在 Twitter Bootstrap Popover,通过ajax动态生成内容 Bootstrap Popover Reinitialization(刷新内容)

Why is it like that? It has been suggested that it works, e.g. in Twitter Bootstrap Popover with dynamically generated content via ajax and Bootstrap Popover Reinitialization (To refresh Content)

当我跳过销毁时,它工作正常,但我不确定当我为元素创建另一个弹出框而不破坏已存在的弹出时会发生什么。它是重新初始化还是创建了一个新的popover而失去了对旧版本的访问权限?

It works just fine when I skip the destroying, but I am not sure what happens when I create another popover for an element without destroying the already existing one. Is it reinitialised or does it create a new popover with losing the access to the old one?

推荐答案

自己解决了。显然 .popover('destroy')是异步的,立即创建另一个弹出窗口失败,而前一个弹出窗口正在被销毁。我尝试使用 alert 添加延迟,但由于某种原因失败了。在创建新的弹出窗口之前使用 setTimeout()并不是最优雅但工作的解决方案:

Solved it myself. Apparently .popover('destroy') is asynchronous, and immediate creation of another popover fails, while the previous one is being destroyed. I tried adding delay by using alert, which failed for some reason. Using setTimeout() before creating new popover is not the most elegant, but working solution:

$('#popoverspan').popover('destroy');
setTimeout(function () {
    $('#popoverspan').popover({
        placement : 'bottom', 
        trigger : 'hover', 
        content : 'Here is new popover!'
    });
}, 200);

200毫秒似乎已经足够了,但在其他情况下可能需要微调。

200 ms seems enough, but it may need finetuning in other cases.

这篇关于Bootstrap popover destroy&每隔一次重新创作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆