Bootstrap工具提示将右侧内容推开

时间:2019-03-06 06:18:42

标签: javascript jquery twitter-bootstrap tooltip

我在表的第二列中添加了tooltip,但这将我的最后一列推开了。

$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip();   
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td data-toggle="tooltip" data-placement="top" title="Hooray!">Defaultson</td>
        <td>def@somemail.com</td>
      </tr>      
      <tr class="success">
        <td>Success</td>
        <td data-toggle="tooltip" data-placement="top" title="Hooray!">Doe</td>
        <td>john@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

4 个答案:

答案 0 :(得分:2)

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td data-toggle="tooltip" data-placement="top" title="Hooray!" data-container="body">Defaultson</td>
        <td>def@somemail.com</td>
      </tr>      
      <tr class="success">
        <td>Success</td>
        <td data-toggle="tooltip" data-placement="top" title="Hooray!" data-container="body">Doe</td>
        <td>john@example.com</td>
      </tr>

    </tbody>
  </table>


</div>

<script>
$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip();   
});
</script>

</body>
</html>

只需添加data-container="body"

<td data-toggle="tooltip" data-placement="top" title="Hooray!" data-container="body">Defaultson</td>

答案 1 :(得分:2)

尝试这种方式:

$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip();   
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td><p data-toggle="tooltip" data-placement="top" title="Hooray!">Defaultson</p></td>
        <td>def@somemail.com</td>
      </tr>      
      <tr class="success">
        <td>Success</td>
        <td><p data-toggle="tooltip" data-placement="top" title="Hooray!">Doe</p></td>
        <td>john@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

答案 2 :(得分:1)

您可以通过以下方式在{em> HTML 中添加data-container="body"

$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip();
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td data-toggle="tooltip" data-container="body" data-placement="top" title="Hooray!">Defaultson</td>
        <td>def@somemail.com</td>
      </tr>      
      <tr class="success">
        <td>Success</td>
        <td data-toggle="tooltip" data-container="body" data-placement="top" title="Hooray!">Doe</td>
        <td>john@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

或可以通过以下方式将{container: 'body'}添加到您的 JavaScript

$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip({container: 'body'});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td data-toggle="tooltip" data-placement="top" title="Hooray!">Defaultson</td>
        <td>def@somemail.com</td>
      </tr>      
      <tr class="success">
        <td>Success</td>
        <td data-toggle="tooltip" data-placement="top" title="Hooray!">Doe</td>
        <td>john@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

编辑:

如果您希望tooltip在文本上而不是td上,则可以创建一个内部元素,例如span并在该项目上添加tooltip。尝试这种方式:

$(document).ready(function(){
  $('[data-toggle="tooltip"]').tooltip();
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Default</td>
        <td>
          <span data-toggle="tooltip" data-container="body" data-placement="top" title="Hooray!">Defaultson</span>
        </td>
        <td>def@somemail.com</td>
      </tr>      
      <tr class="success">
        <td>Success</td>
        <td>
          <span data-toggle="tooltip" data-container="body" data-placement="top" title="Hooray!">Doe</span></td>
        <td>john@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

答案 3 :(得分:0)

一样编辑<tbody>
    <tbody>
  <tr data-toggle="tooltip">
    <td>Default</td>
    <td  data-placement="top" title="Hooray!">Defaultson</td>
    <td>def@somemail.com</td>
  </tr>      
  <tr class="success" data-toggle="tooltip">
    <td>Success</td>
    <td  data-placement="top" title="Hooray!">Doe</td>
    <td>john@example.com</td>
  </tr>
</tbody>

这个。将data-toggle="tooltip"放入<tr>标签