表头中的Bootstrap提示工具?

8

我正在使用默认的Bootstrap工具提示来显示表头。但它表现出不相关的行为,例如增加了表头的宽度。

以下是CodePen链接:

https://codepen.io/tumulalmamun/pen/mpQzBV

$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();   
});
<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.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h3>Tooltip Example</h3>
  <p>The data-placement attribute specifies the tooltip position.</p>
      <div class="row">
      <div class="col-lg-3">
            <table class="table table-bordered table-hover table-striped">
                <thead>
                    <tr><th colspan="2"> status (Overall)</th></tr>
                    <tr>
                        <th colspan="2" data-toggle="tooltip" data-placement="right" title="Hooray!">
                            Pending 

                        </th>

                    </tr>
                    <tr>
                        <th data-toggle="tooltip" data-placement="right" title="Hooray!">M</th>
                        <th data-toggle="tooltip" data-placement="right" title="Hooray!">O</th>
                    </tr>  
                </thead>
</table>
        
      </div>     
     
 <div class="col-lg-3"></div>
      <div class="col-lg-3"></div>

    </div>
</div>

</body>
</html>


你的CodePen与Bootstrap 4无关(正如你的标签所示)。 - WebDevBooster
谢谢您的指出。我在尝试添加Bootstrap,但它没有显示关于Bootstrap的建议。因此我将其移除了。 - Abdullah Al Mamun
1个回答

9
解决方法是将工具提示添加到th/td元素内的一个元素上,而不是直接添加到它上面。 (我已经在您示例中看到的文本周围添加了div元素)
<table class="table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th colspan="5">Pre-Recruitment status (Overall)</th>
        </tr>
        <tr>
            <th rowspan="2" colspan="2">Status </th>
            <th rowspan="2">Candidate</th>
            <th colspan="2">
                <div data-toggle="tooltip" data-placement="right" title="Hooray!">Panding Doc.</div>
            </th>
        </tr>
        <tr>
            <th>
               <div data-toggle="tooltip" data-placement="right" title="Hooray!">M</div>
            </th>
            <th>
                <div data-toggle="tooltip" data-placement="right" title="Hooray!">O</div>
            </th>
        </tr>  
    </thead>
</table> 

当一个工具提示被显示时,它会在带有data-toggle="tooltip"属性的元素之后直接添加一个新的
元素(使用开发工具检查即可看到此过程)。因此,它将破坏你的表格布局,因为工具提示将被放置在表格元素之后。如果在表格元素内部完成,则不会影响布局。
我已经使用解决方案fork了你的codepen。

1
谢谢你的精彩回答和解释。 - Abdullah Al Mamun

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