简体   繁体   English

html标签内的文本溢出

[英]text overflow inside a html tag

I am searching for a solution for text overflow inside a html. 我正在寻找html内文本溢出的解决方案。 in my project I am using angularjs, so basicly I tried to make a directive without any success. 在我的项目中,我使用的是angularjs,因此从根本上讲,我尝试制作指令没有成功。

for example lets say i have the following html tag: 例如,假设我具有以下html标签:

<div style="height=10px;witdh=5px"> text text
sLorem ipsum dolor sit amet, consectetur adipiscing elit. 
Duis quis iaculis tellus, et feugiat orci. Interdum et malesuada 
fames ac ante ipsum primis in faucibus. Vestibulum id condimentum neque. Vivamus nec 
bibendum tortor. Donec mi sapien, rutrum sit amet sagittis eu, consectetur non lectus.   
Cras vitae porttitor assa. Phasellus odio mauris, placerat in luctus nec, malesuada et     
mauris. Donec neque dui, gravida sit amet iaculis vitae, ultrices a enim. Sed mi mauris, 
pharetra eget facilisis id, vestibulum laoreet quam. Etiam interdum nisi non massa 
pellentesque 
</div>

i am looking to get some text from the innerHTML text and then 3 point such as ... if there is a height overflow 我想从innerHTML文本中获取一些文本,然后3点,例如...如果有高度溢出

To accomplish this for your <div> above you'll need the following: 要在上面的<div>完成此操作,您需要执行以下操作:

height: 10px;
width: 5px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

On a slightly different note, your inline CSS is incorrect. 略有不同的是,您的内联CSS不正确。 CSS property-value pairs are separated by a colon like so: CSS属性值对以冒号分隔,如下所示:

<div style="height: 10px, width: 5px, white-space: nowrap, overflow: hidden, text-overflow: ellipsis;">

Here's a quick snippet I just wrote up. 这是我刚写的一个简短摘要。

JSFiddle JSFiddle


This allows for multi-line ellipsis checking by using the width and height of the box and continuing to remove characters until the contents fit inside the box. 这允许通过使用框的宽度和高度并继续删除字符直到内容适合框内的方式来进行多行省略号检查。

$(function(){

    $(".overflow-box").each(function(){

        $("body").append("<div class='overflow-hidden'/>");
        $(".overflow-hidden").css("width", $(this).width()+"px");

        var i = $(this).html().length;
        while($(".overflow-hidden").html($(this).html().substring(0, i--)+"...").height() > $(this).height())
        {}

        $(this).html($(".overflow-hidden").html());
        $(".overflow-hidden").remove();

    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM