我到目前为止:
$content = $("#blog_1").find(".entryContent").text().split(" ");
$slicedContent = $content.slice(30);
我一直在和.split和.slice闲聊几个小时。但没有成功。 我甚至不必“保留”文本的其余部分,因为当用户在“详细信息页面”上单击“阅读更多”时,它将再次加载。从词30开始,文本可以从DOM中删除。 关于显示一定数量的字符而不是单词的答案也很有帮助。
先谢谢!
答案 0 :(得分:3)
首先回答你的问题,就像这样:
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")
更好的是再次限制此文本服务器端的生成。只是明显浪费带宽来发送所有数据并且不使用它。
答案 1 :(得分:0)
这样做:
$slicedContent = $content.slice(0,30)