Home > Web Front-end > JS Tutorial > jquery plug-in development notes_jquery

jquery plug-in development notes_jquery

WBOY
Release: 2016-05-16 18:20:28
Original
1248 people have browsed it

Today I discovered that JQ itself already has a way to store this temporary data:

$("dom").data("mydata","this is data"); Also, when we were developing plug-ins, we It is often necessary to add methods to plug-ins. In fact, just use the method of adding methods inside JS

this.myfn=function(){}

The source code of the previous plug-in that displays part of the text is given below. :

(Similar to CSS text-overflow, but for this plug-in you need to provide a few words to display to accurately control the number of displays)

Copy code The code is as follows:

/**
* 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) "" opt.fill "";
}
$(this ).html(showbody);
}
this.limit(opt.limit);
return this;
}
});
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template