使表格的标题列可左右拉伸jquery插件封装 - Web前端
作者:98wpeu发布时间:2026-05-23分类:网页前端技术浏览:88
插件名称命名为:JQuery.tableresize.JS,代码如下:
复制代码 代码如下:
/*
Writen by mlCACtus, 2014-11-24
这是我封装的一个jQuery插件,能够使table的各列可以左右拉伸,从而使宽度变小或变大
用法:
单个TABle:$("#table_id").tableresize();
页面所有table:$("table").tableresize();
*/
(function ($) {
$.fn.tableresize = function () {
var _document = $("body");
$(this).each(function () {
if (!$.tableresize) {
$.tableresize = {};
}
var _table = $(this);
//设定ID
var id = _table.attr("id") || "tableresize_" + (Math.ranDOM() * 100000).toFixed(0).toString();
var tr = _table.find("tr").first(), ths = tr.children(), _firstth = ths.first();
//设定临时变量存放对象
var cobjs = $.tableresize[id] = {};
cobjs._currentObj = null, cobjs._currentLeft = Null;
ths.mouSEMove(function (e) {
var _this = $(this);
var left = _this.offset().left, top = _this.offset().top, width = _this.width(), height = _this.height(), right = left + width, bottom = top + height, clientX = e.clientX, clIEntY = e.clientY;
var leftside = !_firstth.is(_this) && Math.abs(left - clientX) <= 5, rightside = Math.abs(right - clientX) <= 5;
if (cobjs._currentLeft || clientY > top && clientY < bottom && (leftside || rightside)) {
_document.CSS("cursor", "e-resize");
if (!cobjs._currentLeft) {
if (leftside) {
cobjs._currentObj = _this.PRev();
}
else {
cobjs._currentObj = _this;
}
}
}
else {
cobjs._currentObj = null;
}
});
ths.mouSEOut(function (e) {
if (!cobjs._currentLeft) {
cobjs._currentObj = null;
_document.css("Cursor", "auto");
}
});
_document.mousedown(function (e) {
if (cobjs._currentObj) {
cobjs._currentLeft = e.clientX;
}
else {
cobjs._currentLeft = null;
}
});
_document.mouseup(function (e) {
if (cobjs._currentLeft) {
cobjs._currentObj.width(cobjs._currentObj.width() + (e.clientX - cobjs._currentLeft));
}
cobjs._currentObj = null;
cobjs._currentLeft = null;
_document.css("cursor", "auto");
});
});
};
})(jQuery);
页面代码为:
复制代码 代码如下:
<!DOCtypehtml PUBLIC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTMLxmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css" >
td{ text-align:center;}
</style>
<script type="text/javascript" src="script/jquery-1.10.2.js"></script>
<script type="text/JavaScript" src="script/jquery.tableresize.js"></script>
<script type="text/Javascript">
$(document).ready(function () {
//使两张table同时支持左右拉伸
$("table").tableresize();
});
</script>
</head>
<body>
表格1<br/>
<table cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
<tbody><tr>
<td style="width:200px;">ID</td><td style="width:200px;">名字</td><td style="width:200px;">年纪</td><td style="width:200px;">地址</td><td style="width:200px;">电话</td>
</tr><tr>
<td>22</td><td>Name:44</td><td>Age:23</td><td>Address:47</td><td>Phone:15</td>
</tr><tr>
<td>28</td><td>Name:42</td><td>Age:68</td><td>Address:30</td><td>Phone:50</td>
</tr><tr>
<td>29</td><td>Name:63</td><td>Age:48</td><td>Address:90</td><td>Phone:76</td>
</tr>
</tbody>
</table>
<br/>表格2<br/>
<table cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
<tbody><tr>
<td style="width:200px;">ID</td><td style="width:200px;">名字</td><td style="width:200px;">年纪</td><td style="width:200px;">地址</td><td style="width:200px;">电话</td>
</tr><tr>
<td>22</td><td>Name:44</td><td>Age:23</td><td>Address:47</td><td>Phone:15</td>
</tr><tr>
<td>28</td><td>Name:42</td><td>Age:68</td><td>Address:30</td><td>Phone:50</td>
</tr>
</tbody></table>
</body>
</html>
相关推荐
- 文本有关的样式和jQuery求对象的高宽问题分别说明 - Web前端
- jquery 简单应用示例总结 - Web前端
- 基于jquery实现的定时显示与隐藏div广告的实现代码 - Web前端
- jquery 添加节点的几种方法介绍 - Web前端
- 对于this和$(this)的个人理解 - Web前端
- 使用jQuery和PHP实现类似360功能开关效果 - Web前端
- JS中判断null、undefined与NaN的方法 - Web前端
- jquery批量控制form禁用的代码 - Web前端
- JS中批量给元素绑定事件过程中的相关问题使用闭包解决 - Web前端
- jQuery之折叠面板的深入解析 - Web前端
- 网页前端技术排行
-
- 1jQuery编写widget的一些技巧分享 - Web前端
- 2【第六章】Foundation之按钮和下拉功能 - Web前端
- 3基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 4分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 5在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 9JS网页制作实例:标签云 - 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属性


