1

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

我走了这么远:

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

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

提前感谢!

4

2 回答 2

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")

再好一点是限制这个文本服务器端的生成。将所有数据发送下来而不使用它只是浪费带宽。

于 2012-04-28T19:45:40.793 回答
0

做这个:

$slicedContent = $content.slice(0,30)
于 2012-04-28T19:44:27.087 回答