JQuery implements code for displaying long text

Source: Internet
Author: User

There is only one small area on the webpage, but the descriptive text is very long. The following script displays the long text.
When a user clicks expand, the text is expanded, and the text is merged.

Originally, you can write with the toggle () that comes with jQuery. However, when I do not work, toggle uses the click + flag to do this.
Copy codeThe Code is as follows:
<Script language = "javascript" src = "jquery. js"> </script>
<Script language = "javascript">
Var cur_status = "less ";
$. Extend ({
Show_more_init: function (){
// Alert ("show_more_init! ");
Var charNumbers = $ (". content" character .html (). length; // total words
Var limit = 100; // display words
If (charNumbers> limit)
{
Var orgText = $ (". content" pai.html (); // Original Text
Var orgHeight = $ (". content"). height (); // original height
Var showText = orgText. substring (0, limit); // The final displayed text
$ (". Content" pai.html (showText );
Var contentHeight = $ (". content"). height (); // The height of the captured content
$ (". Switch"). click (
Function (){
If (cur_status = "less "){
$ (". Content" 2.16.height(contentheight).html (orgText). animate ({height: orgHeight}, {duration: "slow "});
Certificate (this).html (" ");
Cur_status = "more ";
} Else {
$ (". Content" 2.16.height(orgheight).html (showText). animate ({height: contentHeight}, {duration: "fast "});
("Expand ");
Cur_status = "less ";
}
}
);
}
Else
{
$ (". Switch"). hide ();
}
}
});
$ (Document). ready (function (){
$. Show_more_init ();
});
</Script>
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> test </title>
<Style>
# Limittext {
Width: 640px;
Height: auto;
Position: relative;
Background-color: # ccc;
Color: black;
}
. Switch {
Font-size: 12px;
Text-align: center;
Cursor: pointer;
Font-family: Verdana;
Font-weight: 800;
Position: absolute;
Bottom: 0;
Width: 100%;
/* Background: url(more-bg.png) repeat-x bottom ;*/
Height: 40px;
Line-height: 80px;
}
</Style>
</Head>
<Body>
<Div id = "limittext">
<Div class = "content" style = "padding-bottom: 15px;">
This is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text, this is a long text, a long text
</Div>
<Div class = "switch"> Expand </div>
</Div>
</Body>
</Html>

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.