A list needs to be displayed during project creation, but the header must be frozen when there is too much data to be rolled, so I wrote the following script (I once found the corresponding script on the Internet, but it is not ideal, so I wrote it myself. But currently, because the project only uses the freeze of the header, you do not need to specify column freezing, so currently you can only calculate an incomplete script, but generally you only need to freeze the header to use it). Now let's take a look:
In this way, the table header is frozen and the content of the following table can be freely rolled.
Check the Code:
// Extension of the clonetableheader Method for jquery CopyCode The Code is as follows: jquery. FN. clonetableheader = function (tableid, tableparentdivid ){
// Obtain the DIV of the frozen header. If the DIV already exists, remove it.
VaR OBJ = Document. getelementbyid ("tableheaderdiv" + tableid );
If (OBJ ){
Jquery (OBJ). Remove ();
}
VaR browsername = navigator. appname; // obtain the browser information, which is used by the following code to differentiate the browser
VaR ver = navigator. appversion;
VaR browserversion = parsefloat (ver. substring (ver. indexof ("MSIE") + 5, Ver. lastindexof ("Windows ")));
VaR content = Document. getelementbyid (tableparentdivid );
VaR scrollwidth = content. offsetwidth-content. clientwidth;
VaR tableorg = jquery ("#" + tableid); // obtain table content
VaR table = tableorg. Clone (); // clone table content
Table. ATTR ("ID", "clonetable ");
// Note: the header to be frozen should be placed in thead.
VaR tableheader = jquery (tableorg). Find ("thead ");
VaR tableheaderheight = tableheader. Height ();
Tableheader. Hide ();
VaR colswidths = jquery (tableorg). Find ("tbody TR: First TD"). Map (function (){
Return jquery (this). Width ();
}); // Dynamically obtain the width of each column
VaR tableclonecols = jquery (table). Find ("thead TR: First TD ")
If (colswidths. Size ()> 0) {// assign a value to the frozen header width based on the browser (mainly to distinguish IE8)
For (I = 0; I <tableclonecols. Size (); I ++ ){
If (I = tableclonecols. Size ()-1 ){
If (browserversion = 8.0)
Tableclonecols. eq (I). Width (colswidths [I] + scrollwidth );
Else
Tableclonecols. eq (I). Width (colswidths [I]);
} Else {
Tableclonecols. eq (I). Width (colswidths [I]);
}
}
}
// Create a div container for freezing the header and set the attributes
VaR headerdiv = Document. createelement ("Div ");
Headerdiv. appendchild (Table [0]);
Jquery(headerdiv).css ("height", tableheaderheight );
Jquery(headerdiv).css ("overflow", "hidden ");
Jquery(headerdiv).css ("Z-index", "20 ");
Jquery(headerdiv).css ("width", "100% ");
Jquery (headerdiv). ATTR ("ID", "tableheaderdiv" + tableid );
Jquery (headerdiv). insertbefore (tableorg. Parent ());
}
The above is the complete code. Now let's take a look at how to use it:
Add the following script to the page.Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Jquery (function (){
Jquery. FN. clonetableheader ("tab1", "div1 ");
});
</SCRIPT>
In this way, the ID of the input table and the DIV where the table is located is OK. Note that the table header to be frozen must be placed in thead; otherwise, the table cannot be frozen.
The preceding Code passes the IE6, 7, and 8 tests. ff and chrome may cause inaccurate header width.
Package and download specific code: Click here to download
PS: If you find it helpful, please recommend it. Thank you!
Author: Yun-yo
Source: http://kyo-yo.cnblogs.com