将Bootstrap提示工具添加到动态创建的按钮

4

I dynamically create buttons like this:

var my_button = document.createElement('button');
my_button.setAttribute('id', "the_id");

我正在尝试在这些按钮上使用Bootstrap工具提示,但是我无法使其正常工作。

my_button.setAttribute('data-toggle', 'tooltip');
my_button.setAttribute('data-placement', 'top');
my_button.setAttribute('title', 'this is my text');

在 $(document).ready(function() 中,我有...
$('[data-toggle="tooltip"]').tooltip();

为了应对按钮是动态创建的事实,我使用:

$('body').tooltip({ selector: '[rel=tooltip]' });

我已经获取了所有必要的Bootstrap文件,并使用文本元素标签测试了工具提示,它可以正常工作。

但是当我悬停在按钮上时没有任何反应。


你有没有仔细阅读我的问题?正如明确说明的那样,这已经尝试过了。 - Cybernetic
1
$('body').tooltip({ selector: '[rel=tooltip]' }); // 我认为这是用于Bootstrap v2的。请尝试使用 $('body').tooltip({ selector: '[data-toggle="tooltip"]' }); 替代。http://codepen.io/partypete25/pen/RaEEPj - partypete25
1个回答

8
在创建dom元素之后调用此脚本:
$('[data-toggle="tooltip"]').tooltip();

刚刚不得不使用jQuery先设置属性...然后像你说的那样添加你的代码行"after"。谢谢! - Cybernetic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接