JQuery 控制內(nèi)容長度超出規(guī)定長度顯示省略號
更新時間:2014年05月23日 14:58:30 作者:
這篇文章主要介紹JQuery如何實現(xiàn)控制內(nèi)容長度超出規(guī)定長度顯示省略號,需要的朋友可以參考下
長度超出規(guī)定長度,顯示省略號
設(shè)置class為displayPart,
設(shè)置自定義屬,displayLength可顯示長度(不包含...),雙字節(jié)字符,長度 *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++; //雙字節(jié)字符,//[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>
設(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>
您可能感興趣的文章:
- jQuery實現(xiàn)控制文字內(nèi)容溢出用省略號(…)表示的方法
- Jquery循環(huán)截取字符串的方法(多出的字符串處理成"...")
- jQuery(js)獲取文字寬度(顯示長度)示例代碼
- 用jquery實現(xiàn)輸入框獲取焦點消失文字
- jquery xMarquee實現(xiàn)文字水平無縫滾動效果
- jQuery計算textarea中文字?jǐn)?shù)(剩余個數(shù))的小程序
- jQuery實現(xiàn)長文字部分顯示代碼
- input 輸入框獲得/失去焦點時隱藏/顯示文字(jquery版)
- jquery實現(xiàn)微博文字輸入框 輸入時顯示輸入字?jǐn)?shù) 效果實現(xiàn)
- jQuery實現(xiàn)文字超過1行、2行或規(guī)定的行數(shù)時自動加省略號的方法
相關(guān)文章
jquery autocomplete自動完成插件的的使用方法
最近剛開始學(xué)jquery,想實現(xiàn)類似GOOGLE搜索時自動顯示出相關(guān)結(jié)果的效果。于是選擇了使用jquery autocomplete插件。2010-08-08jquery slibings選取同級其他元素的實現(xiàn)代碼
jquery選取同級其他元素可以使用slibings方法,end方法可以清除之前的鏈?zhǔn)讲僮?,相?dāng)于重新開始2013-11-11jQuery中對節(jié)點進(jìn)行操作的相關(guān)介紹
本篇文章小編將為大家介紹,在jQuery中對節(jié)點進(jìn)行操作的解決辦法,有需要的朋友可以參考一下2013-04-04以WordPress為例講解jQuery美化頁面Title的方法
鼠標(biāo)移動到超鏈接時顯示Title提示即是所謂Title美化的一般手段,這里我們就以WordPress為例講解jQuery美化頁面Title的方法,需要的朋友可以參考下2016-05-05