function textSubstr(str,sub_length){ str = str.trim(); var temp1 = str.replace(/[^x00-xff]/g,"**");//精髓 var mlength = 22; if(sub_length == undefined){ var temp2 = temp1.substring(0,mlength); //找出有多少个* var x_length = temp2.split("*").length - 1 ; var hanzi_num = x_length /2 ; var sub_length = mlength - hanzi_num ;//实际需要sub的长度是总长度-汉字长度 var res = str.substring(0,sub_length); }else{ var temp2 = temp1.substring(0,sub_length); //找出有多少个* var x_length = temp2.split("*").length - 1 ; var hanzi_num = x_length /2 ; var sub_length = sub_length - hanzi_num ;//实际需要sub的长度是总长度-汉字长度 var res = str.substring(0,sub_length); } if(sub_length < str.length){ var end =res+"..."; }else{ var end = res; } return end; }
字数超过部分用。。。省略