1. In the html file
<div class="sldiv" style="overflow: hidden; word-break: normal; word-wrap: break-word; max-width: 179px">
1111111111111111111111111111111111111111111111111111111111
</div>
1.1 Encapsulated into a function, it is to control the number of words to add an ellipsis
function wordlimit(cname,wordlength){
var cname=document.getElementsByClassName(cname);
console.log(cname);
console.log(cname[0]);
for(var i=0;i<cname.length;i++){
console.log(cname.length);
var nowhtml=cname[i].innerHTML;
var nowlength=cname[i].innerHTML.length;
if(nowlength>wordlength){
cname[i].innerHTML=nowhtml.substr(0,wordlength)+'......';
}
}
}
wordlimit('sldiv',20)
2. In vue
<p class="detail" v-html="item.content"></p>
mounted() {
this.$nextTick(function(){
this.wordlimit('detail', 119)
})
},
2.1 To add a timer, the dom element cannot be obtained without the timer
methods: {
wordlimit(cname, wordlength) {
setTimeout(() => {
let cnamec = document.getElementsByClassName(cname)
for (let i = 0; i < cnamec.length; i++) {
var nowhtml=cnamec[i].innerHTML;
var nowlength=cnamec[i].innerHTML.length;
if(nowlength>wordlength){
cnamec[i].innerHTML=nowhtml.substr(0,wordlength)+'......';
}
}
}, 200)
},
}