JS實現(xiàn)可改變列寬的table實例

           

          <!DOCTYPE HTML> 
          <html> 
          <head> 
          <meta charset="gbk"> 
          <title>table</title> 
          </head> 
          <body> 
          <table id="tb_1" cellspacing="0" cellpadding="2" width="100%" border="1"> 
          <tbody> 
          <tr align="center" bgcolor="#dcdcdc"> 
          <td style="width:100px;">用戶編號</td> 
          <td>試用時間</td><td>轉(zhuǎn)正時間</td><td>性別</td><td>姓名拼音</td> 
          <td>生日時間</td><td>民族</td><td>身高</td> 
          </tr> 
          <tr> 
          <td>2000001</td> 
          <td>1997-3-13</td><td>1997-3-13</td><td>1</td><td>WZJ</td> 
          <td>1965-3-13</td><td></td><td>171</td> 
          </tr> 
          <tr> 
          <td>2000045</td> 
          <td>2001-2-15</td><td>2001-3-15</td><td>0</td><td>WY</td> 
          <td>1978-8-5</td><td></td><td>162</td> 
          </tr> 
          <tr> 
          <td>2000046</td> 
          <td>2001-2-23</td><td>2001-3-23</td><td>0</td><td>LQ</td> 
          <td>2001-2-23</td><td></td><td>171</td> 
          </tr> 
          </tbody> 
          </table> 
          <script type="text/javascript"> 
          var tTD; //用來存儲當(dāng)前更改寬度的Table Cell,避免快速移動鼠標(biāo)的問題 
          var table = document.getElementById("tb_1"); 
          for (j = 0; j < table.rows[0].cells.length; j++
          table.rows[
          0].cells[j].onmousedown = function () 
          //記錄單元格 
          tTD = this
          if (event.offsetX > tTD.offsetWidth - 10
          tTD.mouseDown 
          = true
          tTD.oldX 
          = event.x; 
          tTD.oldWidth 
          = tTD.offsetWidth; 
          }
           
          //記錄Table寬度 
          //
          table = tTD; while (table.tagName != ‘TABLE') table = table.parentElement; 
          //
          tTD.tableWidth = table.offsetWidth; 
          }

          table.rows[
          0].cells[j].onmouseup = function () 
          //結(jié)束寬度調(diào)整 
          if (tTD == undefined) tTD = this
          tTD.mouseDown 
          = false
          tTD.style.cursor 
          = 'default'; 
          }

          table.rows[
          0].cells[j].onmousemove = function () 
          //更改鼠標(biāo)樣式 
          if (event.offsetX > this.offsetWidth - 10
          this.style.cursor = 'col-resize'; 
          else 
          this.style.cursor = 'default'; 
          //取出暫存的Table Cell 
          if (tTD == undefined) tTD = this
          //調(diào)整寬度 
          if (tTD.mouseDown != null && tTD.mouseDown == true
          tTD.style.cursor 
          = 'default'; 
          if (tTD.oldWidth + (event.x - tTD.oldX)>0
          tTD.width 
          = tTD.oldWidth + (event.x - tTD.oldX); 
          //調(diào)整列寬 
          tTD.style.width = tTD.width; 
          tTD.style.cursor 
          = 'col-resize'; 
          //調(diào)整該列中的每個Cell 
          table = tTD; while (table.tagName != 'TABLE') table = table.parentElement; 
          for (j = 0; j < table.rows.length; j++
          table.rows[j].cells[tTD.cellIndex].width 
          = tTD.width; 
          }
           
          //調(diào)整整個表 
          //
          table.width = tTD.tableWidth + (tTD.offsetWidth – tTD.oldWidth); 
          //
          table.style.width = table.width; 
          }
           
          }

          }
           
          </script> 
          </body> 
          </html> 


          posted on 2013-09-01 23:27 我為J狂 閱讀(875) 評論(1)  編輯  收藏 所屬分類: Web

          評論

          # re: JS實現(xiàn)可改變列寬的table實例 2013-11-23 18:16 鳳凰

          很好,能用!謝謝!!  回復(fù)  更多評論   

          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 泰顺县| 武邑县| 祁东县| 沧州市| 徐汇区| 奇台县| 本溪市| 永平县| 罗平县| 上虞市| 兴仁县| 承德县| 武宁县| 玛曲县| 专栏| 宜春市| 扶风县| 板桥市| 镶黄旗| 方正县| 东乌珠穆沁旗| 凌云县| 海丰县| 南宁市| 永仁县| 永福县| 平湖市| 银川市| 乌恰县| 阳西县| 根河市| 修文县| 大悟县| 滦平县| 郎溪县| 梁河县| 九龙城区| 金华市| 茶陵县| 万州区| 前郭尔|