国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JQuery 控制內容長度超出規定長度顯示省略號

2019-11-20 15:12:04
字體:
來源:轉載
供稿:網友
長度超出規定長度,顯示省略號

設置class為displayPart,

設置自定義屬,displayLength可顯示長度(不包含...),雙字節字符,長度 *2,
復制代碼 代碼如下:

<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++; //雙字節字符,//[u4e00-u9fa5]中文

result += _char;
count++;
}
if (result.length < text.length) {
result += "...";
}
this.text(result);
}
});

$(function () {
$(".displayPart").displayPart();
});

</script>

復制代碼 代碼如下:

<!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</h2>

<div style="width:500px;">
hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world!!!
</div>
<hr>
<h2>hello</h2>
<div class="displayPart" displayLength="40"> hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhelloworldhello worldhello worldhello worldhello worldhello worldhello world
</div>
</body>
</html>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长葛市| 陆良县| 加查县| 安国市| 大洼县| 承德县| 古丈县| 高碑店市| 普兰县| 商都县| 中西区| 望江县| 吉林市| 清流县| 扶余县| 沾益县| 泰来县| 循化| 弋阳县| 汪清县| 新宾| 炉霍县| 东平县| 库车县| 胶州市| 宁德市| 随州市| 临湘市| 禹州市| 东城区| 秦安县| 洛隆县| 南华县| 城步| 汤阴县| 石景山区| 栾川县| 西充县| 且末县| 元阳县| 尤溪县|