javascript代码实例教程-jQuery实现拖动调整表格单元格大小的代码实例教程

发布时间:2018-12-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-jQuery实现拖动调整表格单元格大小的代码实例教程脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

jQuery实现的拖动调整表格td单元格的大小:

在实际应用中,可能有这样的需求,那就是需要调整td单元格的大小。
也许是为了便于观察,也许是其他原因,反正这样的需求是有的,下面就分享一段能够实现此功能的代码。

代码实例如下:

 

代码如下:


<!DOCTYPE htML>
<html>
<head>
<;meta charset=" utf-8">
<tITle>博客园</title>
<style type="text/css" >
table {
  border-collapse: collapse;
}
td {
  text-align: center;
}
</style>
<script src="http://libs.baidu.COM/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
(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&amp;&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);
  
$(document).ready(function () {
  $("table").tableresize();
});
</script>
</head>
<body>
<table cellspacing="0" border="1" 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>
</body>
</html>

 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-jQuery实现拖动调整表格单元格大小的代码实例教程全部内容,希望文章能够帮你解决javascript代码实例教程-jQuery实现拖动调整表格单元格大小的代码实例教程所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。