【发布时间】:2018-01-21 19:45:05
【问题描述】:
我正在检测文本何时太长而无法显示,所以如果它很长,我会使用 CSS 减少它们并放置一个包含整个文本的工具提示,但如果它不长,则将其设为绿色。 发生以下情况:
它位于整个文本的中心,而不是短文本。我怎样才能在中心但在短文本中,而不是在长文本中?
如果你想要我的所有代码,这里有:
$(document).ready(function() {
$('[data-toggle="tooltip"]').tooltip();
});
$(function() {
$('.txt').each(function(i) {
if (isEllipsisActive(this)) {
$(this).attr("data-original-title", $(this).text());
} else $(this).css({
'color': 'green'
});
});
});
function isEllipsisActive(e) {
return (e.offsetWidth > 95);
}
.demo {
max-width: 95px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
<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>
<div class="container">
<h3>Tooltip Example</h3>
<div class="demo">
<a class="txt" href="#" data-toggle="tooltip" data-placement="top">Hover over meeeeeeeeeeeeeeee</a>
</div>
<div class="demo">
<a class="txt" href="#" data-toggle="tooltip" data-placement="top">Hover over meeeeeeeeeeeeeeee</a>
</div>
</div>
【问题讨论】:
标签: jquery css twitter-bootstrap twitter-bootstrap-3 tooltip