简体   繁体   English

Bootstrap 4工具提示使用标题以外的自定义属性为工具提示提供文本

[英]Bootstrap 4 tooltip use custom attribute other than title to provide the text for the tooltip

I have an application where I am using Bootstrap 4 with tooltips.我有一个应用程序,我在其中使用带有工具提示的 Bootstrap 4。 I want to toggle the tooltips one by one and show them only on click.我想一一切换工具提示并仅在单击时显示它们。 I am able to do this using:我可以使用:

 <input type="text" id="name" class="form-control" autocomplete="off" 
 data-toggle="tooltip" data-placement="bottom" title="Write name here.">

I am showing the tooltip individually using the following javascript which gets triggered on button click:我正在使用以下 javascript 单独显示工具提示,该工具提示在按钮单击时触发:

 $('#name').tooltip("show");

and I hide/dispose of it using the following script:我使用以下脚本隐藏/处理它:

 $('#name').tooltip("dispose");

The reason I use dispose is because I do not want the tooltip to be visible unless the button is pressed.我使用 dispose 的原因是因为我不希望工具提示可见,除非按下按钮。

The issue is that on mouseover of the input field, the browser (in my case Firefox Developer Edition) shows the tooltip with its own style because the tooltip text is in the title attribute.问题是在输入字段的鼠标悬停时,浏览器(在我的情况下为 Firefox 开发人员版)显示具有自己样式的工具提示,因为工具提示文本位于title属性中。

Is there a way in bootstrap to instead hold the text to be shown in another data-tooltiptitle attribute for eg:在引导程序中有没有办法将文本保存在另一个data-tooltiptitle属性中,例如:

<input type="text" id="name" class="form-control" autocomplete="off" 
data-toggle="tooltip" data-placement="bottom" data-tooltiptitle="Write name here.">

And then maybe tell the tooltip to use the data-tooltiptitle attribute for the text using some jQuery.然后可能告诉工具提示使用一些 jQuery 对文本使用data-tooltiptitle属性。

Any help is appreciated.任何帮助表示赞赏。

You can use a title option and set it as a function.您可以使用title选项并将其设置为 function。 On hover, this function will get called with the instance of the hovered element.在 hover 上,这个 function 将被悬停元素的实例调用。 Using getAttribute , you can then fetch the custom attribute.使用getAttribute ,您可以获取自定义属性。

 $(document).ready(function() { $('[data-toggle="tooltip"]').tooltip({ title: function() { return 'No Hooray;;'. } }): $('#link').tooltip({ title; function() { return this;getAttribute('custom-title'); } }); });
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <a href="#" data-toggle="tooltip" custom-title="Hooray!">Hover over me</a> <input type="text" id="link" custom-title="Hooray!">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM