Use JavaScript to control the width of TD by dragging the mouse

Source: Internet
Author: User

<HTML>
<Head>
<Title> untitled document </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<SCRIPT>
VaR dragenable = false;
VaR X;
Var y;
VaR W;
VaR h;
VaR OBJ;
Function Init (){

X = event. clientx + document. Body. scrollleft;
OBJ = event. srcelement;
W = event. srcelement. offsetwidth;
OBJ. setcapture ();
If (x> event. srcelement. offline setleft + w-5 & x <event. srcelement. offsetleft + W) {dragenable = true; obj. style. cursor = 'e-resize ';}
}

Function drag (){
If (event. clientx + document. body. scrollleft> event. srcelement. offsetleft + event. srcelement. offsetWidth-5 & event. clientx + document. body. scrollleft <event. srcelement. offsetleft + event. srcelement. offsetwidth) {event. srcelement. style. cursor = 'e-resize ';}
Else event. srcelement. style. cursor = 'default ';
If (dragenable = true ){
If (event. clientx + document. Body. scrollleft-x + W> 0 ){
VaR I = obj. cellindex;
VaR J;
For (j = 0; j <obj. parentnode. parentnode. Rows. length; j ++ ){
OBJ. parentnode. parentnode. Rows [J]. cells [I]. width = event. clientx + document. Body. scrollleft-x + W;
}
}
Else {
VaR I = obj. cellindex;
VaR J;
For (j = 0; j <obj. parentnode. parentnode. Rows. length; j ++ ){
OBJ. parentnode. parentnode. Rows [J]. cells [I]. width = 1;
}
}
}
}

Function end (){
Dragenable = false;
OBJ. releasecapture ();
OBJ. style. cursor = 'default ';
}
</SCRIPT>
</Head>

<Body bgcolor = "# ffffff" text = "#000000" leftmargin = 0 topmargin = 0>
The table width is not set or the table width white aspect ratio is displayed.
<Br>
<Table width = "40%" border = "1" cellpadding = "0" cellspacing = "2" onmousedown = Init () onmouseup = end () onmousemove = drag () id = Table1 style = "table-layout: fixed">
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
</Table>

<Br>
Table width is set <br>
<Table border = "1" cellpadding = "0" cellspacing = "2" onmousedown = Init () onmousemove = drag () onmouseup = end () id = Table2 style = "table-layout: fixed" width = "500">
<Tr>
<TD width = "100" Height = "18" align = "TOP"> dffdksdlkfslfkldka; lsdfksd; lkfd; </TD>
<TD width = "100" align = "TOP"> & nbsp; </TD>
<TD width = "100" align = "TOP"> & nbsp; </TD>
<TD width = "100" align = "TOP"> & nbsp; </TD>
<TD width = "100" align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "18" align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
<TD align = "TOP"> & nbsp; </TD>
</Tr>
</Table>

</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.