简体   繁体   English

省略号以截断长文本

[英]Ellipsis to truncate a long text

I am using ellipsis concept to truncate a long text in my HTML. 我正在使用省略号概念截断HTML中的长文本。 I have successfully managed to truncate the sentence but the "..." wont appear in my HTML. 我已成功地截断了该句子,但“ ...”将不会出现在我的HTML中。 I use the following for the css 我将以下内容用于css

The output appears to be fine.. ie, for test test test test test test test test test the out put is test test test when i actually want it as test test test... 输出似乎很好..例如,对于测试测试测试测试测试测试测试测试输出,当我实际上希望将其作为测试测试测试时,输出就是测试测试...

<div style="text-align:left; line-height: 20px; white-space: nowrap;
            overflow: hidden; text-overflow: ellipsis; width: 99%;
            display : block;"> 

I'm not sure what you're asking, or what you are seeing, but the text will only be truncated if there's not enough space in the container. 我不确定您要问的是什么,或者您看到的是什么,但是只有容器中没有足够的空间时,文本才会被截断。 For example: 例如:

<style type="text/css">
div {
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    width: 100px;
}
</style>
<div> 
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

http://jsfiddle.net/jS7ea/ http://jsfiddle.net/jS7ea/

Here I limited the div's width to 100px, and it displays just this: 在这里,我将div的宽度限制为100px,它仅显示以下内容:

Lorem ipsu...

On your example you have 99% width, so you'll only see the truncation if that's less than the actual content width (try resizing the browser window to check). 在您的示例中,您的宽度为99%,因此,如果截断小于实际内容的宽度,则只会看到截断(请尝试调整浏览器窗口的大小以进行检查)。

function shorten(text, maxLength) {
    var ret = text;
    if (ret.length > maxLength) {
        ret = ret.substr(0,maxLength-3) + "...";
    }
    return ret;
}

you can also use &hellip; … 您还可以使用&hellip; … &hellip; … (Horizontal Ellipsis) instead of 3 dots &hellip; … (水平省略号),而不是3点

source: http://html5hub.com/ellipse-my-text 来源: http//html5hub.com/ellipse-my-text

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

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