首先回答你的问题,是这样的:
var test = "I have been messing around with .split and .slice for hours. But no success. I don't even have to 'keep' the remainder of the text, since it will be loaded again when the user will click read more, on the 'detail page'. So onwards from word 30, the text could be removed from the DOM. answers regarding displaying a set number of characters instead of words are also helpful."
var splittest = test.split(' ')
splittest.slice(0, 10)
(returns ["I", "have", "been", "messing", "around", "with", ".split", "and", ".slice", "for"])
我不会这样做。最好限制文本的数量,这样就不必担心必须检测单词的开头/结尾。它还将使造型更容易和更可预测。
test.slice(0, 300)
(returns "I have been messing around with .split and .slice for hours. But no success. I don't even have to 'keep' the remainder of the text, since it will be loaded again when the user will click read more, on the 'detail page'. So onwards from word 30, the text could be removed from the DOM. answers regardi")
再好一点是限制这个文本服务器端的生成。将所有数据发送下来而不使用它只是浪费带宽。