我的CSS:
#content_right_head span
{
display:inline-block;
width:180px;
overflow:hidden !important;
}
现在它显示内容内容
但是我想展示 内容内容…
我需要在内容后面显示点。内容动态地来自数据库。
我的CSS:
#content_right_head span
{
display:inline-block;
width:180px;
overflow:hidden !important;
}
现在它显示内容内容
但是我想展示 内容内容…
我需要在内容后面显示点。内容动态地来自数据库。
当前回答
试试这个,
.truncate {
display:inline-block;
width:180px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
将.truncate class添加到元素中。
编辑,
上述解决方案并不是在所有浏览器中工作。你可以尝试使用jQuery插件跨浏览器支持。
(function ($) {
$.fn.ellipsis = function () {
return this.eachAsync({
delay: 100,
bulk: 0,
loop: function (index) {
var el = $(this);
if (el.data("fullText") !== undefined) {
el.html(el.data("fullText"));
} else {
el.data("fullText", el.html());
}
if (el.css("overflow") == "hidden") {
var text = el.html();
var t = $(this.cloneNode(true))
.hide()
.css('position', 'absolute')
.css('overflow', 'visible')
.width('auto')
.height(el.height())
;
el.after(t);
function width() { return t.width() > el.width(); };
var func = width;
while (text.length > 0 && width()) {
text = text.substr(0, text.length - text.length * 25 / 100);
t.html(text + "...");
}
el.html(t.html());
t.remove();
}
}
});
};
})(jQuery);
如何打电话,
$("#content_right_head span").ellipsis();
其他回答
为此,您可以使用text-overflow: ellipsis;财产。像这样写
跨度{ 显示:inline-block; 宽度:180 px; 空白:nowrap;} 溢出:隐藏!重要; 文本溢出:省略; } Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿了一大堆铅字,把它们胡乱拼凑成一本铅字样本书</span>
JSFiddle
如果您使用的是text-overflow:ellipsis,浏览器将尽可能地显示该容器中的内容。但是,如果您想指定圆点之前的字母数或剥离一些内容并添加圆点,则可以使用下面的函数。
function add3Dots(string, limit)
{
var dots = "...";
if(string.length > limit)
{
// you can also use substr instead of substring
string = string.substring(0,limit) + dots;
}
return string;
}
叫喜欢
add3Dots("Hello World",9);
输出
Hello Wor...
在这里看到它的行动
函数add3Dots(字符串,限制) { Var dots = "…"; 如果字符串。长度>限制) { //你也可以用substr代替substring String = String .substring(0,limit) + dots; } 返回字符串; } console.log(add3Dots("你好,你今天过得怎么样?",10));
如果你正在使用ES6,它可以用三元操作符和模板字面量来创建
function add3Dots(text, limit)
{
return text.length > limit ? `${text.substring(0, limit)}...` : text;
}
你可以试试这个:
.classname { 宽度:250 px; 溢出:隐藏; 文本溢出:省略; }
var tooLong = document.getElementById("longText").value;
if (tooLong.length() > 18){
$('#longText').css('text-overflow', 'ellipsis');
}