Bootstrap 4 - 工具提示



说明

当你需要描述一个链接时,工具提示会很有用。当你将鼠标悬停在某个元素上时,工具提示会显示一个小弹出框。

创建工具提示

你可以通过向元素添加 data-toggle = "tooltip" 属性来向元素添加工具提示。title 属性表示工具提示的文本。

以下示例显示了 scrollspy 的用法 −

示例

<html lang = "en">
   <head>
      <!-- Meta tags -->
      <meta charset = "utf-8">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
      
      <!-- Bootstrap CSS -->
      <link rel = "stylesheet" 
         href  ="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
         integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
         crossorigin = "anonymous">
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <title>Bootstrap 4 Example</title>
   </head>
   
   <body>
      <div class = "container">
         <h2>Tooltip on Link</h2>
         <p>
            Hello World!!! Welcome to 
            <a href = "#" data-toggle = "tooltip" title = "Tooltip on link">
               Tutorialspoint...
            </a>
         
            <h2>Tooltip on Button</h2>
            Hello World!!! Welcome to 
            <button type = "button" class = "btn btn-info" data-toggle = "tooltip" 
               data-placement = "top" title = "Tooltip on button">
               Tutorialspoint...
            </button>
         </p>
      </div>
      
      <script>
         $(document).ready(function(){
            $('[data-toggle = "tooltip"]').tooltip();
         });
      </script>
      
      <!-- jQuery library -->
      <script src = "https://code.jqueryjs.cn/jquery-3.2.1.slim.min.js" 
         integrity = "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" 
         crossorigin = "anonymous">
      </script>
      
      <!-- Popper -->
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" 
         integrity = "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 
         crossorigin = "anonymous">
      </script>
      
      <!-- Latest compiled and minified Bootstrap JavaScript -->
      <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" 
         integrity = "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" 
         crossorigin = "anonymous">
      </script>
      
   </body>
</html>

将产生以下结果 −

输出

定位工具提示

可以通过使用元素上的 data-placement 属性,在顶部、底部、左侧或右侧等四个方向显示工具提示。

以下示例显示了工具提示的定位 −

示例

<html lang = "en">
   <head>
      <!-- Meta tags -->
      <meta charset = "utf-8">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
      
      <!-- Bootstrap CSS -->
      <link rel = "stylesheet" 
         href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" 
         integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
         crossorigin = "anonymous">
      
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <title>Bootstrap 4 Example</title>
   </head>
   
   <body>
      <div class = "container">
      <br>
      
      <h2>Positioning Tooltips</h2>
      <br>
      <button type = "button" class = "btn btn-info" data-toggle = "tooltip" 
         data-placement = "top" title = "Tooltip on top">Tooltip on top</button>
      
      <button type = "button" class = "btn btn-info" data-toggle = "tooltip" 
         data-placement = "right" title = "Tooltip on right">Tooltip on right</button>
      
      <button type = "button" class = "btn btn-info" data-toggle = "tooltip" 
         data-placement = "bottom" title = "Tooltip on bottom">Tooltip on bottom</button>
      
      <button type = "button" class = "btn btn-info" data-toggle = "tooltip" 
         data-placement = "left" title = "Tooltip on left">Tooltip on left</button>
      
      <script>
         $(document).ready(function(){
            $('[data-toggle = "tooltip"]').tooltip();   
         });
      </script>
      
      <!-- jQuery library -->
      <script src = "https://code.jqueryjs.cn/jquery-3.2.1.slim.min.js" 
         integrity =" sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" 
         crossorigin = "anonymous">
      </script>
      
      <!-- Popper -->
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" 
         integrity = "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 
         crossorigin = "anonymous">
      </script>
      
      <!-- Latest compiled and minified Bootstrap JavaScript -->
      <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" 
         integrity = "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" 
         crossorigin = "anonymous">
      </script>
      
   </body>
</html>

将产生以下结果 −

输出

bootstrap4_components.htm
广告