多行文本溢出显示省略号点击展开收缩

效果图
在这里插入图片描述

<div id="goodIntroduction">
    在漫无边际的海水上漂流,偶尔抬头能看见熟悉的日月星辰。因为我不知道下辈子是否还能遇见你,所以我今生才会那么努力把最好的给你。 陪伴,是最长情的告白;而声音,是最温柔的陪伴!
</div>

js

var str = $("#goodIntroduction").text();
 var tempt = str;
 coverup();

 function coverup() {
     if (str.length > 17) {
         $("#goodIntroduction").text(tempt.slice(0, 17) + "...");
     }
     var readmore = "<a οnclick='showmore()'  style='color: orange;'>查看更多↓</a>"
     $("#goodIntroduction").append(readmore);
 }

 function showmore() {
     $("#goodIntroduction").text(str);
     var readmore = "<a οnclick='coverup()'  style='color: orange;'>收起↑</a>"
     $("#goodIntroduction").append(readmore);
 }
发布了167 篇原创文章 · 获赞 52 · 访问量 6913

猜你喜欢

转载自blog.csdn.net/qq_42363032/article/details/103933398