Bootstrap 工具提示 HTML,在 上带有很棒的字体图标锚标记在焦点上不起作用
我有以下 HTML,它按预期工作(当悬停到“!”符号时,会出现工具提示),但是因为其中一个在工具提示内有链接,所以我希望它的行为像“焦点”并且同时也“悬停”,因此当我悬停带有链接的工具提示时,它仍然像“悬停”一样(当我没有悬停 HTML 时工具提示消失,但是当我单击 HTML 时,它会起作用就像“焦点”一样,工具提示仍然如此当我在 HTML 之外单击时出现并消失)
如何实现“焦点”和“悬停”?
这是我正在使用的代码:
HTML 代码
<i class="fa-solid fa-circle-exclamation" data-bs-toggle="tooltip" title="This is a tooltip with no HTML"></i>
<i class="fa-solid fa-circle-exclamation" data-bs-toggle="tooltip" data-bs-html="true" data-bs-trigger="hover focus" title="This is a tooltip with <a href='#'>HTML</a>"></i>
This is a tooltip after HTML
JavaScript 代码
var tooltipTriggerList = [].slice.call(
document.querySelectorAll('[data-bs-toggle="tooltip"]')
);
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
而且,如果我将鼠标悬停在工具提示上,我仍然可以看到“这是 HTML 之后的工具提示”在如下图所示的背景中,如何使工具提示背景为纯色? 上时,不会出现“这是 HTML 之后的工具提示”文本
非常感谢
I have the following HTML, it is working as expected (when hovering to the "!" symbol, the tooltip appears), however as because one of them have the link inside the tooltip, I would like it to act like "focus" and also "hover" at the same time, thus when I hover the tooltip with link inside it, it still act like "hover" (the tooltip disappear when I am not hovering the HTML, but when I click on the HTML, it will act like "focus" which the tooltip still appears and will disappear when I am click outside of the HTML)
How can I achieve it for "focus" and also "hover"?
Here is the code that I am using:
HTML Code
<i class="fa-solid fa-circle-exclamation" data-bs-toggle="tooltip" title="This is a tooltip with no HTML"></i>
<i class="fa-solid fa-circle-exclamation" data-bs-toggle="tooltip" data-bs-html="true" data-bs-trigger="hover focus" title="This is a tooltip with <a href='#'>HTML</a>"></i>
This is a tooltip after HTML
JavaScript Code
var tooltipTriggerList = [].slice.call(
document.querySelectorAll('[data-bs-toggle="tooltip"]')
);
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
and also, if I am hovering to the tooltip, I still can see "This is a tooltip after HTML" in the background like the following image, how can I make the tooltip background is solid? So that the text of "This is a tooltip after HTML" not appear when I am hovering the tooltip
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使 Bootstrap 图标工具提示与悬停和焦点配合使用
要使图标发挥作用,您需要使用可以接收焦点的标签。实现此目的的一种方法是将
标记替换为
标记。图标将显示相同的内容,但现在它可以在悬停和焦点下使用。查看代码片段以查看其工作原理。
如果我理解正确的话,你的第二个问题是问如何在悬停时隐藏工具提示。只需将触发器属性设置为仅聚焦即可完成此操作:
data-bs-trigger="focus"
。请参阅代码片段中的示例。更新
从评论中,第二个问题询问如何更改工具提示背景颜色不透明度。默认值为 0.9,允许工具提示下的文本保持轻微可见。对于 Bootstrap 版本 4 和5、下面的css将背景设置为纯色。对于早期的 Bootstrap 版本,请参阅此相关 SO 问题。
更新 2
原始解决方案添加了
contenteditable
属性以允许将焦点放在元素上。然而,它的副作用是它还允许用户输入文本。为了允许焦点,但禁用用户输入,我们需要将event.preventDefault()
添加到按键和粘贴事件。我们还可以使用 css 隐藏闪烁的文本插入符号,如下所示:完整的工作代码片段
How to make a Bootstrap icon tooltip work with hover and focus
To make the icon work, you will need to use a tag that can receive the focus. One way to do this is to replace the
<i>
tag with a<span contenteditable>
tag. The icon will display the same, but it will now work with both hover and focus. View the code snippet to see it working.Your second question, if I understood correctly, asks how to hide the tooltip on hover. That can be done by simply setting the trigger attribute to focus only:
data-bs-trigger="focus"
. See the example in the snippet.Update
From comments, the second question asks how to change the tooltip background color opacity. The default is 0.9, which allows text under the tooltip to remain slightly visible. For Bootstrap versions 4 & 5, the following css sets the background to solid. For earlier Bootstrap versions, see this related SO question.
Update 2
The original solution added the
contenteditable
attribute to allow focus on the element. However, a side effect of that is that it also allows the user to enter text. To allow focus, but disable user input, we need to addevent.preventDefault()
to the keypress and paste events. We can also hide the blinking text caret with css as shown:Complete working code snippet