표의 제목 열을 좌우로 늘릴 수 있도록 jquery 플러그인을 만들었습니다
6641 단어 jquery 플러그인
플러그인 이름: jquery.tableresize.js, 코드는 다음과 같습니다.
/*
Writen by mlcactus, 2014-11-24
jquery , table ,
:$("#table_id").tableresize();
*/
(function ($) {
$.fn.tableresize = function (options) {
var defaults = {
// table ,
resizeTable: true
};
var opts = $.extend(defaults, options);
var _document = $("body");
// user-select ,
var set_user_select = function (jqueryobj, val) {
jqueryobj.css("-moz-user-select", val).css("-webkit-user-select", val).css("-ms-user-select", val);
};
$(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.outerWidth(), height = _this.outerHeight(), right = left + width, bottom = top + height, pageX = e.pageX, pageY = e.pageY;
var leftside = !_firstth.is(_this) && Math.abs(left - pageX) <= 5, rightside = Math.abs(right - pageX) <= 5;
if (cobjs._currentLeft || pageY > top && pageY < bottom && (leftside || rightside)) {
_document.css("cursor", "e-resize");
set_user_select(_table, "none");
if (!cobjs._currentLeft) {
if (leftside) {
cobjs._currentObj = _this.prev();
}
else {
cobjs._currentObj = _this;
}
}
}
else {
_document.css("cursor", "auto");
cobjs._currentObj = null;
}
});
ths.mouseout(function (e) {
if (!cobjs._currentLeft) {
cobjs._currentObj = null;
_document.css("cursor", "auto");
set_user_select(_table, "auto");
}
});
_document.mousedown(function (e) {
if (cobjs._currentObj) {
cobjs._currentLeft = e.pageX;
}
else {
cobjs._currentLeft = null;
}
});
_document.mouseup(function (e) {
if (cobjs._currentLeft) {
var changeWidth = e.pageX - cobjs._currentLeft;
cobjs._currentObj.width(cobjs._currentObj.width() + changeWidth);
if (opts.resizeTable) {
_table.width(_table.width() + changeWidth);
}
}
cobjs._currentObj = null;
cobjs._currentLeft = null;
_document.css("cursor", "auto");
set_user_select(_table, "auto");
});
});
};
})(jQuery);
페이지 코드는 다음과 같습니다.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="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 ,
var ops = { resizeTable: false };
// table
$("table").tableresize(ops);
});
</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>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
jquery 플러그인 작성 사고방식텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.