赞
踩
提示框是一个小小的弹窗,在鼠标移动到元素上显示,鼠标移到元素外就消失。
通过向元素添加 data-toggle="tooltip" 来来创建提示框。
<a href="#" data-toggle="tooltip" title="提示内容">把鼠标移到到这里</a>
以下实例可以在文档的任何地方使用提示框:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bootstrap 实例</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
- <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
- <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <h3>提示框</h3><br>
- <a href="#" data-toggle="tooltip" title="提示内容">把鼠标移到到这里</a>
- </div>
- <script>
- $(document).ready(function(){
- $('[data-toggle="tooltip"]').tooltip();
- });
- </script>
- </body>
- </html>
运行效果
默认情况下提示框显示在元素上方。
可以使用 data-placement 属性来设定提示框显示的方向: top, bottom, left 或 right:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bootstrap 实例</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
- <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
- <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
- </head>
- <body>
-
- <div class="container">
- <h3>提示框</h3><br>
- <a href="#" data-toggle="tooltip" data-placement="top" title="提示内容">鼠标移动到我这</a>
- <a href="#" data-toggle="tooltip" data-placement="bottom" title="提示内容">鼠标移动到我这</a>
- <a href="#" data-toggle="tooltip" data-placement="left" title="提示内容">鼠标移动到我这</a>
- <a href="#" data-toggle="tooltip" data-placement="right" title="提示内容">鼠标移动到我这</a>
- </div>
-
- <script>
- $(document).ready(function(){
- $('[data-toggle="tooltip"]').tooltip();
- });
- </script>
-
- </body>
- </html>
在按钮中使用:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bootstrap 实例</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
- <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
- <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
- </head>
- <body>
-
- <div class="container">
- <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
- Tooltip on top
- </button>
- <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="right" title="Tooltip on right">
- Tooltip on right
- </button>
- <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">
- Tooltip on bottom
- </button>
- <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="left" title="Tooltip on left">
- Tooltip on left
- </button>
- </div>
-
- <script>
- $(document).ready(function(){
- $('[data-toggle="tooltip"]').tooltip();
- });
- </script>
-
- </body>
- </html>
提示内容添加 HTML 标签,设置 data-html="true",内容放在 title 标签里头:
- <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
- Tooltip with HTML
- </button>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bootstrap 实例</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
- <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
- <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
- </head>
- <body>
-
- <div class="container">
- <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
- Tooltip with HTML
- </button>
- </div>
-
- <script>
- $(document).ready(function(){
- $('[data-toggle="tooltip"]').tooltip();
- });
- </script>
-
- </body>
- </html>
禁用按钮:
- <span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Disabled tooltip">
- <button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
- </span>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bootstrap 实例</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
- <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
- <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
- </head>
- <body>
-
- <div class="container">
- <span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Disabled tooltip">
- <button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
- </span>
- </div>
-
- <script>
- $(document).ready(function(){
- $('[data-toggle="tooltip"]').tooltip();
- });
- </script>
-
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。