JQuery controls that the content length exceeds the specified length to display the ellipsis

Source: Internet
Author: User

The length exceeds the specified length. The ellipsis is displayed.

Set class to displayPart,

Set the custom owner. displayLength can be displayed (not including...), double-byte characters, length * 2,
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$. Fn. extend ({
DisplayPart: function (){
Var displayLength = 100;
DisplayLength = this. attr ("displayLength") | displayLength;
Var text = this. text ();
If (! Text) return "";

Var result = "";
Var count = 0;
For (var I = 0; I <displayLength; I ++ ){
Var _ char = text. charAt (I );
If (count> = displayLength) break;
If (/[^ x00-xff]/. test (_ char) count ++; // double byte character, // [u4e00-u9fa5] Chinese

Result + = _ char;
Count ++;
}
If (result. length <text. length ){
Result + = "...";
}
This. text (result );
}
});

$ (Function (){
$ (". DisplayPart"). displayPart ();
});

</Script>

Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> New Document </title>
<Meta name = "Generator" content = "EditPlus">
<Meta name = "Author" content = "">
<Meta name = "Keywords" content = "">
<Meta name = "Description" content = "">
<Script type = "text/javascript" src = "http://code.jquery.com/jquery-1.9.1.min.js"> </script>
</Head>

<Body>
<H2> hello world
<Div style = "width: 500px;">
Hello worldhello world !!!
</Div>
<Hr>
<H2> hello <Div class = "displayPart" displayLength = "40"> hello worldhello world
</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.