[英]Add Bootstrap 4 tooltip to dynamic button
I want to add a tooltip to a button which could change after the page is loaded. 我想向按钮添加工具提示,该提示在页面加载后可能会更改。
For example. 例如。 I use a button like this:
我使用这样的按钮:
<a href="#" class="btn btn-link">Button active</a>
Now it could be possible, that the button gets disabled and looks like this: 现在有可能该按钮被禁用,如下所示:
<a href="#" class="btn btn-link disabled">Button disabled</a>
I know want to add a tooltip with JS to the disabled button. 我知道要向禁用按钮添加带有JS的工具提示。
I tried this code: 我尝试了这段代码:
$('.btn-link.disabled').tooltip({
title: '<div class="text-left">Please select color first</div>',
html: true,
});
That doesn't work. 那不行 I think because the class
disabled
is new and the script for the tooltip couldn't find it when the page loads. 我认为是因为
disabled
的类是新的,并且在页面加载时工具提示的脚本找不到。 Is there a way to "reload" the function? 有没有办法“重新加载”功能?
Here is a simple way to do what you are asking. 这是执行您要问的简单方法。 You can update it to your specific use case but this should get you started.
您可以将其更新为您的特定用例,但这应该可以帮助您入门。
$('.btn-link.disabled').wrap('<span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Please select color first"></span>');
<a href="#" class="btn btn-link">Button active</a> <a href="#" class="btn btn-link disabled">Button disabled</a> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.