8.6.3 工具提示的事件

8.6.3 工具提示的事件

Bootstrap为工具提示提供了如下事件。

事件 描述
show.bs.tooltip 当工具提示开始显示时触发该事件。
inserted.bs.tooltip 当工具提示的HTML模板加入DOM文档时
shown.bs.tooltip 当工具提示显示完成时触发该事件。
hide.bs.tooltip 当工具提示开始被隐藏时触发该事件。
hidden.bs.tooltip 当工具提示隐藏完成时触发该事件。

程序示例

下面代码示范了工具提示的事件触发顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> 工具提示事件 </title>
<link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../bootstrap/css/bootstrap-theme.min.css">
<script type="text/javascript" src="../jquery-3.1.1.js"></script>
<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
<!-- 要是用工具提示必须设置data-toggle="tooltip"属性 -->
<!-- title属性中放提示信息 -->
<button class="btn btn-info" data-toggle="tooltip" title="工具提示事件">
工具提示事件</button>
</div>
<script type="text/javascript">
$(function () {
// 所有提示信息在下方显示
$("[data-toggle='tooltip']").tooltip({
placement: "bottom"
});
$("[data-toggle='tooltip']").on('show.bs.tooltip', function () {
console.log("show.bs.tooltip");
}).on('shown.bs.tooltip', function () {
console.log("shown.bs.tooltip");
}).on('hide.bs.tooltip', function () {
console.log("hide.bs.tooltip");
}).on('hidden.bs.tooltip', function () {
console.log("hidden.bs.tooltip");
}).on('inserted.bs.tooltip', function () {
console.log("inserted.bs.tooltip");
})
});
</script>
</body>

</html>

当用户把鼠标移动到按钮上时,工具提示显示出来;当用户把鼠标从按钮上移开时,工具提示被隐藏起来。在这个过程中,工具提示触发事件的顺序如下所示:

1
2
3
4
5
show.bs.tooltip                         tooltip-event.html:28:13
inserted.bs.tooltip tooltip-event.html:36:13
shown.bs.tooltip tooltip-event.html:30:13
hide.bs.tooltip tooltip-event.html:32:13
hidden.bs.tooltip tooltip-event.html:34:13