Automatic Text truncation Based on Jquery (source code is provided)

Source: Internet
Author: User

Plug-in requirements (functional requirements)
A plug-in is to complete a specific function. When creating a plug-in, we should determine which functions should be available after the plug-in is developed.
One morning, Boot, connect to the database, start the VS development environment, and debug the program in the mode. The program is running, but some content on the page exceeds the permitted range of the page. This is not easy, SubString,
Yes, this is indeed a good way to solve this kind of problem, but when the page needs to be processed to interact with the content, this method will inevitably be a bit uncomfortable, let's use Jquery to develop a plug-in that meets this requirement;
Development instructions
If you are not very connected to the process of developing plug-ins using Jquery, please refer to this article: developing the Jquery plug-in (1) (including final)
One-step development of Jquery plug-in-Automatic Text Scaling
First, we should think that in order to expand the plug-in the future, the conditions should not be written into the program, so the plug-in should have the following parameters: Length (Limit Length), Replace (the replaced text), ShowMore (show all buttons), and HideMore (hide excessive text );
1. When Jquery develops a plug-in, it provides parameters to the plug-in and uses the default defined parameters. The general syntax is as follows:
Copy codeThe Code is as follows:
$. Fn. MyFunction = function (options) {// options is the input parameter array;
Var defaults = {
Arg1 :...,
Arg2 :"...",
ArgN :"",
Replace :"..."
};
Var options = $. extend (defaults, options );

For the plug-in we developed today, the corresponding plug-in parameters are as follows:
Copy codeThe Code is as follows:
$. Fn. HideMore = function (options ){
Var defaults = {
Length: 10,
Showmore: "more ",
Hidemore: "hide ",
Replace :"..."
};
Var options = $. extend (defaults, options );

2. The subsequent work process is as follows:
I. Get the content length in the Div;
Ii. Comparison with the length value passed to the plug-in;
Iii. If the length exceeds the length, it is truncated and replaced;
Iiii. Define events for showmore and hidemore;
Plug-in source code:
Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. HideMore = function (options ){
Var defaults = {
Length: 10,
Showmore: "more ",
Hidemore: "hide ",
Replace :"..."
};
Var options = $. extend (defaults, options );
Var objhtml = require (this).html ();
If (objhtml. length> options. length ){
Var precontent = objhtml. substring (0, options. length );
Var lastcontent = "" + objhtml. substring (options. length, objhtml. length) + "";
Var morelink = "" + options. showmore + "";
Var newcontent = precontent + lastcontent +
Options. replace + morelink;
Publish (thisdomain.html (newcontent );
$ (". More" mirror.css ("display", "none ");
$ (". Morelink"). click (function (){
If ($ (". morelink" ).html () = options. showmore ){
$ (". More"). show (1000 );
$ (". Morelink" pai.html (options. hidemore );
Return false;
}
Else {
$ (". More"). hide (900 );
$ (". Morelink" pai.html (options. showmore );
Return false;
}
});
}
Return false;
};
}) (JQuery );

Html test code:
Copy codeThe Code is as follows:
$ ("Elements"). HideMore (
{
Length: 50,
Showmore "Exhibition ",
Hidemore "shrink ",
Replace :"......"
});

Now, you can COPY and run the code here. Your second plug-in was born.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.