如何自动重新绑定引导工具提示?

How can I automatically rebind bootstrap tooltips?

本文关键字:工具提示 绑定 何自动 新绑定      更新时间:2023-09-26

使用twitter引导工具提示,我可以做一些像

$('.tooltip-enabled').tooltip();

<div id="1234" class="tooltip-enabled"</div>中工作良好。

但是,如果我接着执行$("#1234").replaceWith('<div id="1234" class="tooltip-enabled"</div>');,绑定就丢失了。

我知道我可以在replaceWith之后重新绑定它,但我想避免这种情况,如果我可以的话(主要是这样我就不需要记住每次都这样做,或者我设置了哪些选项!)。有没有一种方法可以做到这一点,就像.on()的工作原理一样?

Chain tooltip() method:

$("#1234").replaceWith('<div id="1234" class="tooltip-enabled"></div>').tooltip();
更好的是,如果你只是在这里添加一个类,使用addClass():
$("#1234").addClass("tooltip-enabled").tooltip();

如果你想做事件委托,使用selector option (demo):

$("body").tooltip({
  selector: ".tooltip-enabled"
});
$("#1234").addClass("tooltip-enabled");

您可以尝试container选项。它会将tooltip附加到另一个对象上,并对DOM的更改做出反应,就像委托事件一样:

$('.tooltip-enabled').tooltip({
    container: 'body'
});

尝试将'{live: true}'作为参数传递给函数,例如

$('.tooltip-enabled').tooltip({live: true})

作为函数

的参数