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>