我有很长的标题,我想 chop 它们,但以一种没有单词中断的方式,我的意思是切割发生在单词之间而不是切割一个单词。
我如何使用 jquery 来实现?
最佳答案
来自: jQuery text truncation (read more style)
试试这个:
var title = "This is your title";
var shortText = jQuery.trim(title).substring(0, 10)
.split(" ").slice(0, -1).join(" ") + "...";
你也可以使用插件:
作为字符串的扩展
String.prototype.trimToLength = function(m) {
return (this.length > m)
? jQuery.trim(this).substring(0, m).split(" ").slice(0, -1).join(" ") + "..."
: this;
};
用作
"This is your title".trimToLength(10);
关于javascript - 如何 chop jQuery 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637942/