<分区>
我正在寻找一个可以用省略号 (...) chop 字符串的简单脚本
我想将 'this is a very long string'
chop 为 'this is a ve...'
我不想使用 CSS 或 PHP。
<分区>
我正在寻找一个可以用省略号 (...) chop 字符串的简单脚本
我想将 'this is a very long string'
chop 为 'this is a ve...'
我不想使用 CSS 或 PHP。
最佳答案
function truncate(input) {
if (input.length > 5) {
return input.substring(0, 5) + '...';
}
return input;
};
或者在 ES6 中
const truncate = (input) => input.length > 5 ? `${input.substring(0, 5)}...` : input;
关于javascript - 我想使用 JavaScript chop 带有省略号的文本或行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4700226/