先回答你的問題,是這樣的:
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")
再好的一點是限制這個文本服務器端的生成。這只是一個簡單的帶寬浪費,無法發送所有數據。
您是否試過閱讀[文檔?](https://developer.mozilla.org/zh/JavaScript/Reference/Global_Objects/Array/slice) – 2012-04-28 19:46:24