截断文本以适合 3 行并在 Html 中显示三个点

Sun*_*uja 5 html javascript jquery-mobile

我需要将文本显示为段落,但只显示三行并在段落末尾添加 ...(三个点)时截断测试。

Isr*_*imu 15

.class-name {
        display: block;
        white-space: nowrap;
        width: 15em;
        overflow: hidden;
        text-overflow: ellipsis;
    }
Run Code Online (Sandbox Code Playgroud)
    <div style="height: 15vh; padding: 10px;
                background:#dee0e5">
    <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum is simply dummy text of the printing and typesetting industry</span>
    </div>

<div style="height: 15vh;padding: 10px; margin-top:5px;
                background:#aff8af">
    <span class="class-name">Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum is simply dummy text of the printing and typesetting industry</span>
    </div>
Run Code Online (Sandbox Code Playgroud)


zav*_*avg 12

计算可以放入 3 行的字符串的最大长度并使用以下脚本

var maxLength = 140;
var result = yourString.substring(0, maxLength) + '...';
Run Code Online (Sandbox Code Playgroud)


alo*_*ica 9

我使用与@zavg 相同的逻辑,并进行了一些改进:

  • 使用单个字符。
  • 尊重最大尺寸。

function truncate(source, size) {
  return source.length > size ? source.slice(0, size - 1) + "…" : source;
}

var res = truncate('Truncate text to fit in 3 lines', 14);
console.log(res);
Run Code Online (Sandbox Code Playgroud)


clo*_*ure 1

尝试以下 css 属性:

.your-class-name {
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  line-clamp: 3;
}
Run Code Online (Sandbox Code Playgroud)

  • 你需要发布一个演示,因为我认为这没有任何作用。 (5认同)