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

首頁 > 語言 > JavaScript > 正文

JQuery 控制內(nèi)容長度超出規(guī)定長度顯示省略號

2024-05-06 16:06:33
字體:
供稿:網(wǎng)友
這篇文章主要介紹JQuery如何實(shí)現(xiàn)控制內(nèi)容長度超出規(guī)定長度顯示省略號,需要的朋友可以參考下

長度超出規(guī)定長度,顯示省略號

設(shè)置class為displayPart,

設(shè)置自定義屬,displayLength可顯示長度(不包含...),雙字節(jié)字符,長度 *2,

復(fù)制代碼 代碼如下:


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

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

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

</script>


復(fù)制代碼 代碼如下:


<!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 content="EditPlus">
<meta content="">
<meta content="">
<meta content="">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>

<body>
<h2>hello world</h2>

<div>
hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world!!!
</div>
<hr>
<h2>hello</h2>
<div displayLength="40"> hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhelloworldhello worldhello worldhello worldhello worldhello worldhello world
</div>
</body>
</html>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 昔阳县| 曲阳县| 玉树县| 慈利县| 凌云县| 石柱| 哈巴河县| 吉安县| 郓城县| 太康县| 远安县| 奉贤区| 忻州市| 南召县| 蕲春县| 阿拉尔市| 密云县| 靖西县| 孟村| 西宁市| 石渠县| 莱州市| 兴安盟| 太谷县| 建宁县| 景东| 石屏县| 桃园市| 彩票| 乌鲁木齐县| 周口市| 霍山县| 桓台县| 宁城县| 凤庆县| 武城县| 南丰县| 永丰县| 资中县| 湄潭县| 沅江市|