时间:2021-07-01 10:21:17 帮助过:11人阅读
/**
* demo:
* 1.$("#limittext").limittext();
* 2.$("#limittext").limittext({"limit":1});
* 3.$("#limittext").limittext({"limit":1,"fill":"(部分隐藏)","fillid":"aaa"});
* 4.$("#limittext").limittext({"limit":1,"fill":"(部分隐藏)","fillid":"aaa"}).limit(10); * 5.$("#limittext").limittext({"limit":1,"fill":"(部分隐藏)","fillid":"aaa"}).limit('all');
* @param {Object} opt
* @author Lonely * @link http://liushan.net
*/
jQuery.fn.extend({
limittext:function(opt){
opt=$.extend({
"limit":30,
"fill":"...",
"fillid":null
},opt);
var $this=$(this);
var body=$(this).data('body');
if(body==null){
body=$this.html();
$(this).data('body',body);
}
this.limit=function(limit){
if(body.length<=limit||limit=='all')
var showbody=body;
else{
if(opt.fillid==null)
var showbody=body.substring(0,limit)+opt.fill;
else
var showbody=body.substring(0,limit)+"<span id='"+opt.fillid+"'>"+opt.fill+"<span>";
}
$(this).html(showbody);
}
this.limit(opt.limit);
return this;
}
});