猿问

截断长字符串的智能方法

有没有人比显而易见的解决方案/库更复杂的解决方案/库,用于使用JavaScript截断字符串并在末尾添加省略号:


if(string.length > 25) {

    string = string.substring(0,24) + "...";

}


翻过高山走不出你
浏览 366回答 3
3回答

婷婷同学_

所有其他浏览器都支持CSS解决方案:p {&nbsp; &nbsp; white-space: nowrap;&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* IE6 needs any width */&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* "overflow" value must be different from&nbsp; visible"*/&nbsp;&nbsp; &nbsp; -o-text-overflow: ellipsis;&nbsp; &nbsp; /* Opera < 11*/&nbsp; &nbsp; text-overflow:&nbsp; &nbsp; ellipsis;&nbsp; &nbsp; /* IE, Safari (WebKit), Opera >= 11, FF > 6 */}具有讽刺意味的是,我从Mozilla MDC获得了该代码段。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答