JQuery控制内容长度超出规定长度显示省略号
2023-06-13 09:15:27 时间
长度超出规定长度,显示省略号
设置class为displayPart,
设置自定义属,displayLength可显示长度(不包含...),双字节字符,长度*2,
复制代码代码如下:
设置class为displayPart,
设置自定义属,displayLength可显示长度(不包含...),双字节字符,长度*2,
<scripttype="text/javascript">
$.fn.extend({
displayPart:function(){
vardisplayLength=100;
displayLength=this.attr("displayLength")||displayLength;
vartext=this.text();
if(!text)return"";
varresult="";
varcount=0;
for(vari=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>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>NewDocument</title>
<metaname="Generator"content="EditPlus">
<metaname="Author"content="">
<metaname="Keywords"content="">
<metaname="Description"content="">
<scripttype="text/javascript"src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<h2>helloworld</h2>
<divstyle="width:500px;">
helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld!!!
</div>
<hr>
<h2>hello</h2>
<divclass="displayPart"displayLength="40">helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld
</div>
</body>
</html>
相关文章
- jQuery实现轮播效果
- jquery中load中文乱码的解决方法
- jQuery创建json对象
- jQuery图片切换插件jquery.cycle.js详解编程语言
- jQuery温习篇强大的JQuery选择器
- Jquery工作常用实例使用AJAX使网页进行异步更新
- jQuery控制图片的hover效果(smartRollover.js)
- jquery创建一个新的节点对象(自定义结构/内容)的好方法
- js/jQuery对象互转(快速操作dom元素)
- jQuery学习笔记(2)--用jquery实现各种模态提示框代码及项目构架
- jQuery学习笔记(4)--Jquery中获取table中某列值的具体思路
- jQuery使用一个按钮控制图片的伸缩实现思路
- 使用JQUERY进行后台页面布局控制DIV实现左右式
- js与jquery获取父级元素,子级元素,兄弟元素的实现方法
- jQuery学习笔记之总体架构
- 使用jquery+CSS实现控制打印样式
- JQuery控制div外点击隐藏而div内点击不会隐藏的方法
- jQuery通过控制节点实现仅在前台通过get方法完成参数传递
- jQuery控制TR显示隐藏的三种常用方法
- jQuery针对各类元素操作基础教程