js 控制超出字数显示省略号

//多余显示省略号
function wordlimit(cname, wordlength) {
    var cname = document.getElementsByClassName(cname);
    for (var i = 0; i < cname.length; i++) {      
        var nowLength = cname[i].innerHTML.length;
        if (nowLength > wordlength) {
            cname[i].innerHTML = cname[i].innerHTML.substr(0, wordlength) + '...';
        }      
    } 
};

//调用,description为class名
wordlimit('description', 30)

posted @ 2018-03-08 09:33  abcByme  阅读(5217)  评论(3编辑  收藏  举报