我有超过 500 个字符的段落。我只想获得最初的 100 个字符并隐藏其余的字符。我还想在 100 个字符旁边插入“更多”链接。单击更多链接时,整个段落应显示并将文本“更多”编辑为“更少”,单击“更少”时,它应切换行为。段落是动态生成的,我无法使用 .wrap() 包装它的内容。这是我所拥有的和我想要的示例。
这就是我所拥有的:
<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>
当我们点击“Less”时,它应该恢复点击“More”时所做的。
我正在使用 jQuery 将子字符串拆分、切片和包装到我想隐藏的跨度中,但这不起作用。
var title = $("p").text();
var shortText = jQuery.trim(title).substring(100, 1000).split(" ")
.slice(0, -1).join(" ") + "...More >>";
shortText.wrap('</span>');