You can drag the JS table column to the special effect:
[Html]
<! DOCTYPE html>
<Html>
<Head>
<Style type = "text/css">
Table {
Border-top: solid 1px black;
Border-left: solid 1px black;
Font-size: 12px;
Width: 100%;
}
Th {
White-space: nowrap;
}
Th, td {
Border-right-style: solid;
Border-right-width: 1px;
Border-bottom-style: solid;
Border-bottom-width: 1px;
Height: 30px;
}
. Dragable {
Width: 3px;
Height: 100%;
Background-color: white;
Float: right;/* This style has a certain relationship with the effect. Others do not matter */
Cursor: col-resize;
}
</Style>
<Script type = "text/javascript">
Var draging = false; // whether to drag
Var dragElement = null; // The th
Var offsetLeft = 0; // The th and absolute left coordinates of the current drag
Var offsetWidth = 0; // The absolute width of th currently dragged
Function mousedown (){
If (draging) return;
Draging = true;
DragElement = window. event. srcElement. parentNode;
OffsetLeft = dragElement. offsetLeft;
Document. body. style. cursor = "col-resize ";
Document. body. onselectstart = function () {return false ;}; // when dragging or dropping a word, it is not allowed to be selected when the mouse slides over the word (the effect of the selected word by default is known to everyone)
}
Function mouseup (){
Draging = false;
DragElement = null;
Document. body. style. cursor = "auto ";
Document. body. onselectstart = function () {return true };
}
Function mousemove (){
If (draging & dragElement ){
Var width = event. clientX-offsetLeft;
If (width> 0 ){
DragElement. style. width = width;
OffsetWidth = dragElement. offsetWidth;
} Else {
DragElement. style. width = offsetWidth;
}
}
}
</Script>
</Head>
<Body onmousemove = "mousemove ();" onmouseup = "mouseup ();">
<Table cellpadding = "0" cellspacing = "0">
<Thead>
<Tr>
<Th style = "width: 120px;"> NO. <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span> </th>
<Th style = "width: 120px;"> name <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span> </th>
<Th style = "width: 120px;"> age <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span> </th>
<Th> remarks </th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td> 1 </td>
<Td> Siuon </td>
<Td> 100 </td>
<Td> JavaEE engineer... </td>
</Tr>
</Tbody>
</Table>
</Body>
</Html>
Dynamically generate <span class = "dragable" onmousedown = "mousedown ();"> & nbsp; </span>
[Html]
<! DOCTYPE html>
<Html>
<Head>
<Style type = "text/css">
Table {
Border-top: solid 1px black;
Border-left: solid 1px black;
Font-size: 12px;
Width: 100%;
}
Th {
White-space: nowrap;
}
Th, td {
Border-right-style: solid;
Border-right-width: 1px;
Border-bottom-style: solid;
Border-bottom-width: 1px;
Height: 30px;
}
. Dragable {
Width: 3px;
Height: 100%;
Background-color: white;
Float: right;/* This style has a certain relationship with the effect. Others do not matter */
Cursor: col-resize;
}
</Style>
<Script type = "text/javascript">
Var draging = false; // whether to drag
Var dragElement = null; // The th
Var offsetLeft = 0; // The th and absolute left coordinates of the current drag
Var offsetWidth = 0; // The absolute width of th currently dragged
Function mousedown (){
If (draging) return;
Draging = true;
DragElement = window. event. srcElement. parentNode;
OffsetLeft = dragElement. offsetLeft;
Document. body. style. cursor = "col-resize ";
Document. body. onselectstart = function () {return false ;}; // when dragging or dropping a word, it is not allowed to be selected when the mouse slides over the word (the effect of the selected word by default is known to everyone)
}
Function mouseup (){
Draging = false;
DragElement = null;
Document. body. style. cursor = "auto ";
Document. body. onselectstart = function () {return true };
}
Function mousemove (){
If (draging & dragElement ){
Var width = event. clientX-offsetLeft;
If (width> 0 ){
DragElement. style. width = width;
OffsetWidth = dragElement. offsetWidth;
} Else {
DragElement. style. width = offsetWidth;
}
}
}
// Create <span class = "dragable" onmousedown = "mousedown ();"> </span>
Function createSpan (){
Var span = document. createElement ("span ");
Span. className = "dragable ";
Span. attachEvent ("onmousedown", mousedown );
Span. innerHTML = "";
Return span;
}
Function init (){
// Add span in th
Var ths = document. getElementsByTagName ("th ");
For (var I = 0; I <ths. length; I ++ ){
Ths [I]. appendChild (createSpan ());
}
}
</Script>
</Head>
<Body onload = "init ()" onmousemove = "mousemove ();" onmouseup = "mouseup ();">
<Table id = "mytable" cellpadding = "0" cellspacing = "0">
<Thead>
<Tr>
<Th style = "width: 120px;"> NO. </th>
<Th style = "width: 120px;"> name </th>
<Th style = "width: 120px;"> age </th>
<Th> remarks </th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td> 1 </td>
<Td> Siuon </td>
<Td> 100 </td>
<Td> JavaEE engineer... </td>
</Tr>
</Tbody>
</Table>
</Body>
</Html>
From Siuon's Blog