时间:2021-07-01 10:21:17 帮助过:37人阅读
function worldLimit(className,factLength){
$('.'+className).each(function(){
var content = $(this).text();
var allLength = 0;
for (var i = 0; i < content.length; i++) {
var c = content.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
allLength++;
}
else {
allLength += 2;
}
}
if(allLength > factLength){
var subContent = '';
var subLength = 0;
var factLength = 0;
for (var i = 0; i < content.length; i++) {
var c = content.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
subLength++;
}
else {
subLength += 2;
}
factLength ++;
if(subLength > factLength){
break;
}else{
subContent = content.substring(0,factLength);
}
}
$(this).text(subContent);
$(this).html($(this).html()+'......');
}
});
}