Bootstrap 3中Glyphicon的提示文本

33

我不确定这是否可能,我正在尝试在输入组中激活一个图标上的工具提示,我的代码(不起作用)是:

<div class="input-group">
<input type="text" class="form-control" name="security" id="security"><span class="input-group-addon"><span class="glyphicon glyphicon-info-sign" data-toggle="tooltip" data-container: 'body' title="" data-original-title="Security Code"></span></span>
</div>

我正在使用Bootstrap 3和最新版本的jQuery。


3
jQuery的“latest”版本是一个不断变化的目标。请具体说明。 - Diodeus - James MacFarlane
5个回答

77

您可以使用 title 属性(在您的示例中为空白)在 glyphicon 上获得简单的工具提示。

title="info"

可运行的代码

 // add this in your js
 // all the glyphicons having the class "my-tooltip" will show a tooltip if "title" attribute is present
 $(".my-tooltip").tooltip();

 <span class='glyphicon glyphicon-info-sign my-tooltip' title="here goes the tooltip text"></span>

22

使用 @MadJack 的答案,我重写了您的代码,并在 JSFiddle 上进行了测试。

HTML:

<div class="input-group" style='margin-top: 100px;'>
    <input type="text" class="form-control" name="security" id="security">
        <span class="input-group-addon">
            <a class='my-tool-tip' data-toggle="tooltip" data-placement="left" title="Tooltip here">
                <!-- The class CANNOT be tooltip... -->
                <i class='glyphicon glyphicon-info-sign'></i>
            </a>
        </span>
    </input>
</div>

JS:

$("a.my-tool-tip").tooltip();
希望这能有所帮助, JSFiddle。 - Andrew

5

我是一名新手,涉及Bootstrap技术...

如果要将提示工具插件与图标一起使用,我发现可以将图标的span标签用没有href属性的锚点a标签包裹起来:

<a data-toggle="tooltip" class="tooltipLink" data-original-title="Tooltip text goes here">
  <span class="glyphicon glyphicon-info-sign"></span>
</a>

此外,请确保为具有工具提示的链接初始化工具提示:

$("a.tooltipLink").tooltip();

我在使用Bootstrap 3.03、jQuery 1.10.2 + Firefox 26、Chrome 32.0.1700和IE 11.0.9600进行了测试,目前没有出现任何问题。


5
<span class="glyphicon glyphicon-info-sign icon_info" title="info"></span>

然后添加JavaScript代码来初始化工具提示,如下所示:
$(".icon_info").tooltip();

3
使用Bootstrap的Popover效果更佳,这里是文档和示例代码片段。

$(function () {
  $('.fa').popover({trigger: "hover"});
})
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Information Popover snippet">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Information Popover snippet</title>
  <script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
</head>
<body>  
  <span><i data-content="Simple Clean way to show more detailed information about anything" class="fa fa-question-circle"></i></span>
</body>
</html>


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