length exceeding specified length, showing ellipsis
Set class to Displaypart,
Set custom genus, Displaylength can display length (does not contain ...) ), double-byte characters, length *2,
Copy Code code 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 characters,//[u4e00-u9fa5] Chinese
result = _char;
count++;
}
if (Result.length < text.length) {
result = "...";
}
This.text (result);
}
});
$ (function () {
$ (". Displaypart"). Displaypart ();
});
</script>
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<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>
<body>
<div style= "width:500px;" >
Hello Worldhello worldhello worldhello worldhello worldhello worldhello worldhello Worldhello worldhello Worldhello World Hello World!!!
</div>
<div class= "Displaypart" displaylength= "a" > Hello Worldhello worldhello worldhello-Worldhello Worldhello Worldhello Worldhello Worldhelloworldhello Worldhello Worldhello worldhello Worldhello World
</div>
</body>