JavaScript to hide and omit text effects

Source: Internet
Author: User

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB2312"/>
<Title> JS </title>
</Head>
<Body>
<Div id = "content"> one kind of mentality is put down; one kind of realm is willing; one kind of happiness is waiting; one kind of wisdom is low-key; and the other is giving up; there is a kind of understanding that is confused; there is a kind of mentality that is inclusive; there is a kind of happiness that is simple; there is a kind of virtue that is called smile; there is a kind of happiness that is cherished; there is a kind of beauty that is confidence; there is a kind of emotion called sharing; there is a kind of truth called caring; there is a kind of warmth called gratitude; there is a kind of success called persistence.
</Div>
 
<Script language = "javascript"> www.2cto.com
(Function (){
Var len = 50; // The default number of words displayed
Var content = document. getElementById ("content"); // content gets the content div object
Var text = content. innerHTML; // text is the content
Var span = document. createElement ("span"); // create a SPAN tag
Var n = document. createElement ("a"); // create A tag
Span. innerHTML = text. substring (0, len); // the content of the SPAN tag is the first len character of text.
N. innerHTML = text. length> len? ".. Expand": ""; // the content of the created A tag. If the number of words in the content exceeds len, it is "... expand". Otherwise, it is null.
N. href = "javascript: void (0)"; // set the link address of tag A (random)
N. onclick = function () {// click the link to execute the following function:
// If the content of tag A is "... expand", the content of tag A is changed to "collapse", and the content of tag SPAN is all the content of DIV; otherwise, the content is the first len character.
If (n. innerHTML = "... expand "){
N. innerHTML = "hide ";
Span. innerHTML = text;
} Else {
N. innerHTML = "... expand ";
Span. innerHTML = text. substring (0, len );
}
}
Content. innerHTML = ""; // sets the DIV content to null.
Content. appendChild (span); // Add the SPAN element to the DIV.
Content. appendChild (n); // Add Element A to the DIV
})();
</Script>
</Body>
</Html>
:

From Lee.'s column
 

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.