国产探花免费观看_亚洲丰满少妇自慰呻吟_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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巫山县| 恭城| 阳信县| 三江| 棋牌| 桑植县| 和静县| 泰兴市| 承德市| 汕尾市| 吐鲁番市| 博爱县| 车险| 龙州县| 拉孜县| 旬阳县| 浪卡子县| 贺州市| 孟津县| 民丰县| 延津县| 从江县| 弋阳县| 岗巴县| 澄江县| 万源市| 明光市| 富锦市| 永城市| 蒙阴县| 兴业县| 荔波县| 永平县| 清河县| 襄城县| 陇南市| 孝感市| 黄浦区| 成武县| 湘潭市| 米易县|