简体   繁体   English

仅当文本多于x行时显示文本

[英]Display text only if text is more than x lines

I wish to output some text to a table cell. 我希望将一些文本输出到表格单元格。 If this text is more than x lines I trim it and add a "see more" button. 如果此文本多于x行,则将其修剪并添加“查看更多”按钮。

Up to here everything is great, the problem is that the "see more" button is there also when I have a y<x lines of text. 到目前为止,一切都很好,问题是当我有y<x行文本时,“查看更多”按钮也在那里。

Is there a way to check whether the text is more\\less than x lines? 有没有一种方法可以检查文本是否多于x行?

you can have the line number based on the height of the element, if one line have 10px height 10 lines have 100px height Vanilla js: 您可以根据元素的高度获得行号,如果一行的高度为10px,则10行的高度为100px。Vanilla js:

let height = document.getElementById('IdElement').offsetHeight;

Jquery library: jQuery库:

let height = $('#IdElement').height();

with this variable you can do the logic to find out the amount of lines 使用此变量,您可以进行逻辑运算以找出行数

to directly have the line number you need to put each line in a tag be it p , span , whatever Perhaps the easiest is actually to take this with the height of the element 要直接获得行号,您需要将每行放入标签pspan或其他中,也许最简单的方法实际上是将其与元素的高度相乘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM