A list needs to be displayed during project creation in a period of time, but the header must be frozen when there is too much data to scroll, so the following script is written.(I used to find the corresponding script on the Internet, but I wrote it myself because it is not ideal. But currently, 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.
CheckCode:
// Extension of the clonetableheader Method for jquery
Jquery. FN. clonetableheader = function (tableid, tableparentdivid) {// obtain the DIV of the frozen header. If the DIV already exists, remove var OBJ = document. getelementbyid ("tableheaderdiv" + tableid); If (OBJ) {jquery (OBJ ). remove ();} var browsername = navigator. appname; // obtain the browser information, which is used in subsequent code to distinguish 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 the table content var table = tableorg. clone (); // clone the 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 () ;}); // obtain the width of each column dynamically. var tableclonecols = jquery (table ). find ("thead TR: First TD") if (colswidths. size ()> 0) {// the browser assigns a value to the width of the frozen header (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 property 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.
<SCRIPT type = "text/JavaScript"> jquery (function () {jquery. FN. clonetableheader ("tab1", "div1") ;}); </SCRIPT>
In this way, the ID of the DIV where the input table and table are located is OK,Note that the 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!