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

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

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

最新評論

郑州市| 长岛县| 德保县| 藁城市| 红河县| 杂多县| 邢台县| 无锡市| 宣化县| 海林市| 安宁市| 两当县| 乌鲁木齐县| 宿松县| 开封县| 宝山区| 肥西县| 绥芬河市| 大冶市| 万山特区| 乌恰县| 喀喇| 宽甸| 工布江达县| 罗平县| 德钦县| 越西县| 随州市| 通河县| 新绛县| 宜川县| 泉州市| 静安区| 安西县| 揭阳市| 信丰县| 高雄县| 翁源县| 化德县| 开江县| 平泉县|