Jquery freezes the header (IE6, passed the test)

Source: Internet
Author: User

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!

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.