js字数限制方法
新手,求大佬们轻虐
// 字体超出限定字数显示...
function testAuto(thisClass,needLeng){
var classs = document.getElementsByClassName(thisClass);
for(var i=0; i<classs.length; i++){
if((classs[i].innerText.length)>= needLeng){
var nowWord =classs[i].innerText.substr(0,needLeng)+'...';
classs[i].innerText = nowWord;
};
};
};
调用方法
testAuto(需要限制字数的类名,字数长度);
列如:testAuto(“news-right-title”,54);
注:英文和中文的字数长度不一,使用时请注意字符数