我有500多個字符的段落。我只想得到最初的100個字符並隱藏其餘部分。另外我想在100個字符旁邊插入「更多」鏈接。點擊更多鏈接整個段落應顯示和編輯文本「更多」到「更少」,並點擊「更少」它應該切換行爲。段落是動態生成的,我無法用.wrap()包裝它的內容。這裏是我擁有的和我想要的例子。截取段落前100個字符並隱藏段落的其餘內容顯示/隱藏其他鏈接的鏈接
這是我有:
<p>It is a long established fact that a reader will be distracted by the readable
content of a page when looking at its layout. The point of using Lorem Ipsum is that
it has a more-or-less normal distribution of letters, as opposed to using 'Content
content here', making it look like readable English. Many desktop publishing packages
and web page editors now use Lorem Ipsum as their default model text.</p>
這就是我想要什麼,當DOM加載
<p>It is a long established fact that a reader will be distracted by ..More</p>
這就是我想要的東西,當用戶點擊 「更多」
<p>It is a long established fact that a reader will be distracted by the readable
content of a page when looking at its layout. The point of using Lorem Ipsum is that
it has a more-or-less normal distribution of letters, as opposed to using 'Content
content here', making it look like readable English. Many desktop publishing packages
and web page editors now use Lorem Ipsum as their default model text. ..Less</p>
當我們點擊「較少」時,它應該恢復點擊「更多」所做的事情。
我正在使用jQuery來分割,切片並將子字符串換成跨度,我想要隱藏但是不起作用。
var title = $("p").text();
var shortText = jQuery.trim(title).substring(100, 1000).split(" ")
.slice(0, -1).join(" ") + "...More >>";
shortText.wrap('</span>');
你試過了什麼?也許你可以發佈一些代碼來顯示你的嘗試 – 2012-07-10 16:18:13
我已經添加了代碼示例。 var shortText我想把它包裝成span標籤。但.wrap()不起作用。 – 2012-07-10 16:27:26