js封装的一行半显示省略号。(字数自由控制)
2021-07-20 04:06
标签:on() .text jquery break set function query res extend $(function() { }) js封装的一行半显示省略号。(字数自由控制) 标签:on() .text jquery break set function query res extend 原文地址:http://www.cnblogs.com/kelly2017/p/7056688.html
//控制一行半隐藏
(function ($) {
$.fn.displayPart = function (opts) {
$(this).each(function () {
displayPart($(this), opts);
});
}
function displayPart($el, opts) {
var defaults = {
displayLength: 50 //具体的想要显示的字数
}
this.Settings = $.extend(defaults, opts);
if (this.Settings.displayLength == defaults.displayLength && $el.data("textcount")) {
this.Settings.displayLength = $el.data("textcount");
}
var displayLength = this.Settings.displayLength;
var text = $el.text();
if (!text) return "";
var result = "";
var count = 0;
for (var i = 0; i var _char = text.charAt(i);
if (count >= displayLength) break;
if (/[^x00-xff]/.test(_char)) count++; //双字节字符,//[u4e00-u9fa5]中文
result += _char;
count++;
}
if (result.length result += "...";
}
$el.text(result);
}
})(jQuery);
$("p").displayPart();
//控制一行半隐藏
文章标题:js封装的一行半显示省略号。(字数自由控制)
文章链接:http://soscw.com/index.php/essay/106471.html