JQuery 控制内容长度超出规定长度显示省略号 - Web前端
作者:98wpeu发布时间:2026-06-19分类:网页前端技术浏览:10
导读:长度超出规定长度,显示省略号设置class为displayPart,设置自定义属,displaylength可显示长度(不包含...),双字节字符,长度*2,复制代码代码...
长度超出规定长度,显示省略号
设置class为displayPart,
设置自定义属,displaylength可显示长度(不包含...),双字节字符,长度 *2,
复制代码 代码如下:
<scripttype="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++; //双字节字符,//[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>
设置class为displayPart,
设置自定义属,displaylength可显示长度(不包含...),双字节字符,长度 *2,
复制代码 代码如下:
<scripttype="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++; //双字节字符,//[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>
相关推荐
- jQuery $命名冲突解决方案汇总 - Web前端
- 推荐25个超炫的jQuery网格插件 - Web前端
- 用jquery模仿的a的title属性的例子 - Web前端
- 一款基jquery超炫的动画导航菜单可响应单击事件 - Web前端
- jQuery前端框架easyui使用Dialog时bug处理 - Web前端
- JQuery设置获取下拉菜单某个选项的值(比较全) - Web前端
- Jquery设置attr的disabled属性控制某行显示或者隐藏 - Web前端
- jQuery判断checkbox是否选中的3种方法 - Web前端
- 事件委托与阻止冒泡阻止其父元素事件触发 - Web前端
- jQuery截取指定长度字符串代码 - Web前端
- 网页前端技术排行
-
- 1【第六章】Foundation之按钮和下拉功能 - Web前端
- 2jQuery编写widget的一些技巧分享 - Web前端
- 3在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 4基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 5分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 8JS网页制作实例:标签云 - Web前端
- 9分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


