Bootstrap“ tooltip”和“ popover”在表格中添加额外的大小

注:
根据您的 Bootstrap 版本(3.3之前或不) ,您可能需要一个不同的答案。
注意笔记。

当我激活此代码中的工具提示(悬停在单元格上方)或弹出窗口时,表的大小正在增加。我怎样才能避免这种情况呢?

这里是 emptyRow-函数,用100生成 tr

<html>
<head>
<title></title>
<script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<script type="text/javascript" language="javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.1/bootstrap.min.js"></script>
<style>
#matrix td {
width: 10px;
height: 10px;
border: 1px solid gray;
padding: 0px;
}
</style>
<script>
function emptyRow() {
str = '<tr>'
for (j = 0; j < 100; j++) {
str += '<td rel="tooltip" data-original-title="text"></td>'
}
str += '</tr>'
return str
}
$(document).ready(function () {
$("#matrix tr:last").after(emptyRow())
$("[rel=tooltip]").tooltip();
});
</script>
</head>
<body style="margin-top: 40px;">
<table id="matrix">
<tr>
</tr>
</table>
</body>
</html>

谢谢你的建议!

79410 次浏览

Note: Solution for Bootstrap 3.0 ~ 3.2

You need to create an element inside a td and apply a tooltip to it, like this, because a tooltip itself is a div, and when it is placed after a td element it brakes table layout.

This problem was introduced with the latest release of Bootstrap. There are ongoing discussions about fixes on GitHub here. Hopefully the next version includes the fixed files.

I would like to add some precision to the accepted answer, I decided to use the answer format for readibility.

Note: Solution for Bootstrap 3.0 ~ 3.2

Right now, wrapping your tooltip in a div is the solution, but it will need some modifications if you want your whole <td> to show the tooltip (because of Bootstrap CSS). A simple way to do it is to transfert <td>'s padding to wrapper :

HTML

<table class="table table-hover table-bordered table-striped">
<tr>
<td>
<div class="show-tooltip" title="Tooltip content">Cell content</div>
</td>
</tr>
</table>

JS (jQuery)

$('.show-tooltip').each(function(e) {
var p = $(this).parent();
if(p.is('td')) {
/* if your tooltip is on a <td>, transfer <td>'s padding to wrapper */
$(this).css('padding', p.css('padding'));
p.css('padding', '0 0');
}
$(this).tooltip({
toggle: 'toolip',
placement: 'bottom'
});
});

Note: Solution for Bootstrap 3.3+

Simple Solution

In the .tooltip() call, set the container option to body:

$(function () {
$('[data-toggle="tooltip"]').tooltip({
container : 'body'
});
});

Alternatively you can do the same by using the data-container attribute:

<p data-toggle="tooltip" data-placement="left" data-container="body" title="hi">some text</p>

Why does this work?

This solves the problem because by default, the tooltip has display: block and the element is inserted in the place it was called from. Due to the display: block, it affects the page flow in some cases, i.e pushing other elements down.

By setting the container to the body element, the tooltip is appended to the body instead of where it was called from, so it doesn't affect other elements because there is nothing to "push down".

Note: Solution for Bootstrap 3.3+

If you want to avoid to break the table when applying a tooltip to a <td> element, you could use the following code:

    $(function () {
$("body").tooltip({
selector: '[data-toggle="tooltip"]',
container: 'body'
});
})

You html could look like this:

<td data-toggle="tooltip" title="Your tooltip data">
Table Cell Content
</td>

This even works with dynamically loaded content. For example in use with datatables

You should initialize Tooltip inside datatable function fnDrawCallback

"fnDrawCallback": function (data, type, full, meta) { $('[data-toggle="tooltip"]').tooltip({ placement: 'right', title: 'heyo', container: 'body', html: true }); },

And define your column as below

 {
targets: 2,
'render': function (data, type, full, meta) {
var htmlBuilder = "<b>" + data + "</b><hr/><p>Description: <br/>" + full["longDescrioption"] + "</p>";
return "<a href='#' class='Name'>" + (data.length > 50 ? data.substr(0, 50) + '…' : data) + "</a>" +
"<sup data-toggle='tooltip' data-original-title=" + htmlBuilder + ">"+
"<i class='ic-open-in-new ic' style='font-size:12px;margintop:-3px;'></i></sup>";
}
},

If you are using datatable for table then it will be use full

$('#TableId').DataTable({
"drawCallback": function (settings) {
debugger;
$('[data-toggle="tooltip"]').tooltip({
container: 'body'
});


}
});

If you're using bootstrap directives for AngularJS, use tooltip-append-to-body attribute.

<td ng-repeat="column in row.columns" uib-tooltip="\{\{ ctrl.viewModel.leanings.tooltip }}" tooltip-append-to-body="true"></td>