Html 适用于 span 标签的 Twitter Bootstrap 工具提示(不适用于标签)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15023565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:49:26  来源:igfitidea点击:

Twitter Bootstrap tooltip for span tag (not for a tag)

htmltwitter-bootstraptooltip

提问by Haradzieniec

My question is based on SO question Bootstrap tooltips not working

我的问题基于 SO question Bootstrap tooltips not working

So, the solution is to use: jQuery:

所以,解决方案是使用:jQuery:

$("[rel='tooltip']").tooltip();

html:

html:

<a href="#" rel="tooltip" title="A nice tooltip">test</a>

And that works perfect. My question is: what if I want to use a <span>tag instead of <a>.

这很完美。我的问题是:如果我想使用<span>标签而不是<a>.

Should it be <span rel="tooltip" title="A nice tooltip">Hover on me</span>?

应该是<span rel="tooltip" title="A nice tooltip">Hover on me</span>吗?

It works perfectly but I see the relattribute is used for a tags.

它工作得很好,但我看到该rel属性用于标签。

So, what's the correct solution?

那么,正确的解决方法是什么?

回答by Shail

Your can write in the following ways :

您可以通过以下方式编写:

Method 1

方法一

  <a title="" data-placement="top" data-toggle="tooltip" href="#" data-original-title="Tooltip on top">Tooltip on top</a>

And Than initialize it with the following code :

然后用以下代码初始化它:

$('a').tooltip();

The other way is to use classes or ids and initialize them one by one like :

另一种方法是使用类或 id 并逐一初始化它们,例如:

Method 2

方法二

 <span class="top" title="A nice tooltip" data-original-title="Tooltip on right">Hover on me</span>      

<a class="top" title=""  href="#" data-original-title="Tooltip on top">Tooltip on top</a>

Than initializing it with the following code :

比用以下代码初始化它:

$(".top").tooltip({
placement: "top"
});

Jsfiddle Method 1

Jsfiddle方法1

Jsfiddle Method 2

Jsfiddle方法2

回答by Dunhamzzz

If you don't want to use a relattribute on a <span>, simply use something else like a class and update the tooltip selector, eg:

如果您不想在 arel上使用属性<span>,只需使用其他类似类的东西并更新工具提示选择器,例如:

$("[rel='tooltip'], .tooltip").tooltip();

<span class="tooltip" title="A nice tooltip">Hover on me</span>

You can use any CSS selector inside the jQuery function $()to select any element on the page in any way.

您可以使用 jQuery 函数内的任何 CSS 选择器$()以任何方式选择页面上的任何元素。