最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年05月23日 14:58:30   作者:  
這篇文章主要介紹JQuery如何實現(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 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>

相關(guān)文章

最新評論

广东省| 乐昌市| 衢州市| 晋州市| 故城县| 塔河县| 冀州市| 泰来县| 福建省| 甘泉县| 金坛市| 泰顺县| 施秉县| 视频| 芦溪县| 通许县| 从化市| 霍林郭勒市| 南岸区| 威宁| 乳源| 沙河市| 梧州市| 民县| 漳平市| 宣威市| 佛冈县| 高台县| 襄城县| 芦山县| 台东县| 临高县| 乾安县| 延川县| 泉州市| 华坪县| 绥滨县| 临澧县| 徐水县| 山西省| 沁水县|