只显示第一个#words,使用jquery

时间:2012-04-28 19:37:59

标签: split slice

我有一个问题。我从数据库加载一些文本。在我的页面上,我只想显示本文的30个第一个单词。然后,我将添加一个“read more”按钮,该按钮将用户重定向到新页面。因此,我认为'jquery扩展器插件'将显示同一个div中的剩余文本,对我来说不起作用。

我到目前为止:

$content = $("#blog_1").find(".entryContent").text().split(" ");
$slicedContent = $content.slice(30);

我一直在和.split和.slice闲聊几个小时。但没有成功。 我甚至不必“保留”文本的其余部分,因为当用户在“详细信息页面”上单击“阅读更多”时,它将再次加载。从词30开始,文本可以从DOM中删除。 关于显示一定数量的字符而不是单词的答案也很有帮助。

先谢谢!

2 个答案:

答案 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)