【发布时间】:2021-09-27 14:59:06
【问题描述】:
我有一个字符串,我截断它以适合卡片,但是当文本中有空格时它非常适合,但是当没有空格时它只会溢出Just like this
我的截断代码如下所示
function truncateString(string, maxLength) {
console.log(`${string.length} and ${maxLength}`)
return string.substring(0, maxLength) + "...";
}
可能是什么原因? 有什么替代方案?
当我在 CSS 中使用 text-overflow 时,它不会覆盖高度 just like this
我的 HTML 代码外观
<div class="dateDiv">
<p class="infoTxt">${noteObj.date}</p>
</div>
<div class="titleDiv">
<p class="title">${truncateString(noteObj.title,15)}</p>
</div>
<div class="contentDiv">
<p class="regularTxt">${truncateString(noteObj.note,188)}</p>
</div>
<div class="authorDiv">
<p class="infoTxt">${noteObj.author}</p>
</div>
<i class="delBtn fas fa-trash-alt fa-lg"></i>
<div class="noteOverlay"></div>
【问题讨论】:
-
不要在 JavaScript 中这样做,使用 css css-tricks.com/almanac/properties/t/text-overflow
-
请分享 HTML
-
这能回答你的问题吗? How to prevent text from overflowing in CSS?
-
当我在 CSS 中使用 text -overflow 时,它不会用文本覆盖总高度
标签: javascript css truncate