前台截取一定长度的后台返回文字,并在后面加省略号

来源:互联网 发布:如何提高淘宝心的级别 编辑:程序博客网 时间:2024/05/18 03:47

RT.

不废话,直接上代码.

window.onload = function(){        var text = document.getElementById('infoText'),                str = text.innerHTML,                textLeng = 150;  //这里设置需要截取的文字的长度.        if(str.length > textLeng ){            text .innerHTML = str.substring(0,textLeng )+"...";        }    }

这种处理方法是在文字比较多且需要换行的情况,如果不需要换行,可以直接通过

overflow:hidden;white-space:nowrap;text-overflow:ellipsis;

css设定来实现.当然容器需要设定好固定的宽度.

0 0