1

私は問題があります。dbからテキストをロードします。私のページでは、このテキストの最初の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 に答える