标签:
function ellipsis(val, len, word) { if (val && val.length > len) { if (word) { var vs = val.substr(0, len - 1) var i = Math.max(vs.lastIndexOf(‘ ‘), vs.lastIndexOf(‘.‘), vs.lastIndexOf(‘!‘), vs.lastIndexOf(‘?‘)) if ( i !== -1 && i >= (len-15) ) { return vs.substr(0, i) + ‘...‘ } } return val.substr(0, len) + ‘...‘ } return val }
调用方式:
var aa="你是谁?萨达哈撒"; var bb=ellipsis(aa,5); console.log(bb);
标签:
原文地址:http://www.cnblogs.com/xiaozhumaopao/p/5891559.html