简体   繁体   English

如果文本的长度大于某个值,如何添加阅读更多按钮

[英]How to add a readmore button if the length of a text is greater than a value

I am trying to show a read more button if a length of a comment is greater than 80 characters.如果评论的长度大于 80 个字符,我会尝试显示阅读更多按钮。 this is how i check it这就是我检查它的方式

<tr repeat.for="m of comments">
<td if.bind="showLess">${m.comment.length < 80 ? m.comment:m.comment.substr(0,80) + " ... "}</td>
</tr>

so if its greater than 80 it shows the "..." but at the end of the dots i wanted to add a button so i tried this所以如果它大于 80 它会显示“...”但是在点的末尾我想添加一个按钮所以我尝试了这个

 <td if.bind="showLess">${m.comment.length < 80 ? m.comment:m.comment.substr(0,80) + " ...<button>Read More</button> "}</td>

but then it messes up my html structure and shows it as但随后它弄乱了我的 html 结构并将其显示为

在此处输入图像描述

how do i structure the html correctly with in ${this function}?如何在 ${this function} 中正确构建 html?

i cant add the button after the $ { } because then it will show up even if the characters are less than 80.我无法在 $ { } 之后添加按钮,因为即使字符少于 80 个,它也会显示出来。

Note: i didnt use ellipsis because i need the character length注意:我没有使用省略号,因为我需要字符长度

Not sure if this will help but I think using character length is not a good idea here is why:不确定这是否会有所帮助,但我认为使用字符长度不是一个好主意,原因如下:

iiiiiiiiiiiiiiiiiiii... iiiiiiiiiiiiiiiiiiiii...

mmmmmmmmmmmmmmmmmmmm...嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯...

Both of these lines have 20 characters but the width of the line is drastically different (unless you are using a font which has similar width chars).这两行都有 20 个字符,但行的宽度截然不同(除非您使用的字体具有相似的宽度字符)。 You can do this with CSS with number of lines with following code:您可以使用带有以下代码的行数的 CSS 来执行此操作:

display: '-webkit-box',
'-webkit-line-clamp': <number of lines>,
'-webkit-box-orient': 'vertical',
overflow: 'hidden',

Replace number of lines with number of lines of text in that particular div.用该特定 div 中的文本行数替换行数。

For more or less you can use a span tag instead of a button, give it position absolute and place it after the text.或多或少您可以使用 span 标签而不是按钮,给它绝对位置并将其放在文本之后。

in Aurelia if.bind is a conditional for deciding if the tag and its contents should be displayed.在 Aurelia 中 if.bind 是决定是否应显示标签及其内容的条件。 So based on what you have written, what you're saying is.因此,根据您所写的内容,您要说的是。 'If showLess is true, then show this tag and its contents' '如果 showLess 为真,则显示此标签及其内容'

it seems what you want is 'if the length of this string is more than 80 chars, then truncate it and show an elipses'看来你想要的是'如果这个字符串的长度超过 80 个字符,然后截断它并显示一个省略号'

I'm out of practice with Aurelia, but you want something similar to this.我没有使用 Aurelia,但你想要类似的东西。

 <td>${m.comment.length < 80 ? m.comment : m.comment.substr(0,80)} <a if.bind=`${m.comment.length > 80}` href=# click.delegate="someFunc()">...</a></td>

Truncate the string if it's too long, then show the ellipses if the string is too long.如果字符串太长,则截断字符串,如果字符串太长,则显示省略号。 Also, don't use a button if you don't have to, it'll look bad.另外,如果不需要,不要使用按钮,它看起来很糟糕。

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

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