使用JavaScript处理
const text = '测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字'
function textTruncation(text,len){
if(text.length>len){
return text.substring(0,len)+"..."
}else{
return text
}
}
console.log(textTruncation(text,5))
使用css处理
<style>
.text{
width:200px;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
</style>
<div class="">
测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字
</div>