当前位置: 编程技术>WEB前端
JQuery 控制内容长度超出规定长度显示省略号
来源: 互联网 发布时间:2014-08-25
本文导语: 长度超出规定长度,显示省略号 设置class为displayPart, 设置自定义属,displayLength可显示长度(不包含...),双字节字符,长度 *2, 代码如下: $.fn.extend({ displayPart:function () { var displayLength = 100; displayLength = this.attr("displayLength") ...
长度超出规定长度,显示省略号
设置class为displayPart,
设置自定义属,displayLength可显示长度(不包含...),双字节字符,长度 *2,
$.fn.extend({
displayPart:function () {
var displayLength = 100;
displayLength = this.attr("displayLength") || displayLength;
var text = this.text();
if (!text) return "";
var result = "";
var count = 0;
for (var i = 0; i < displayLength; i++) {
var _char = text.charAt(i);
if (count >= displayLength) break;
if (/[^x00-xff]/.test(_char)) count++; //双字节字符,//[u4e00-u9fa5]中文
result += _char;
count++;
}
if (result.length < text.length) {
result += "...";
}
this.text(result);
}
});
$(function () {
$(".displayPart").displayPart();
});
New Document
hello world
hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world!!!
hello
设置class为displayPart,
设置自定义属,displayLength可显示长度(不包含...),双字节字符,长度 *2,
代码如下:
$.fn.extend({
displayPart:function () {
var displayLength = 100;
displayLength = this.attr("displayLength") || displayLength;
var text = this.text();
if (!text) return "";
var result = "";
var count = 0;
for (var i = 0; i < displayLength; i++) {
var _char = text.charAt(i);
if (count >= displayLength) break;
if (/[^x00-xff]/.test(_char)) count++; //双字节字符,//[u4e00-u9fa5]中文
result += _char;
count++;
}
if (result.length < text.length) {
result += "...";
}
this.text(result);
}
});
$(function () {
$(".displayPart").displayPart();
});
代码如下:
New Document
hello world
hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world!!!
hello
hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhelloworldhello worldhello worldhello worldhello worldhello worldhello world