Use CSS code to generate Table Fixed header www.865171.cn <script> <br> //document.all.myt.insertAdjacentHTML("afterBegin","<h1>Insert content in the container before the text</h1>"); <br> // document.all.myt.insertAdjacentHTML("beforeEnd","<h2>Insert content in the container after the text</h2>"); <br> //document.all.myt.insertAdjacentHTML("beforeBegin"," <h2>Insert content in the container after the text</h2>"); <br> //document.all.myt.insertAdjacentHTML("beforeBegin","<div id='scrollDiv' class='scrollDiv' >"); <br> <br> </script>
?????????????????????????????????????????????
Just a simple fixed header page. I want to add a DIV or SPAN tag in front of the table and implement it later with JS (annotated below because it cannot achieve the effect I want). I have been looking for it for a long time but I can't find a solution. Does anyone know how to solve it?
The effect I want is that in the code of the page posted above, there is no DIV before the table tag. It needs to be appended in the subsequent JS, and then the effect of fixing the table header can also be achieved. I am pure CSS, JS novice, I hope someone can help me solve it!
Reply to discussion (solution) Use jquery’s warp method to outsource a div, as follows
1
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
><html xmlns=
"http://www.w3.org/1999/xhtml"
><head><meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/><title>无标题文档</title><style type=
"text/css"
> <!-- body,table, td, a {font:9pt;}
.scrollRowThead{position: relative; left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);z-index:0;}
.scrollCR { z-index:3;}
.scrollDiv {height:200px;clear: both; border: 1px solid #EEEEEE;OVERFLOW: scroll;width: 300px; }
.scrollColThead td,.scrollColThead th{ text-align: center ;}
.scrollRowThead,.scrollColThead td,.scrollColThead th{background-color:#FFFFFF;}
.scrolltable{border-bottom:1px solid #CCCCCC; border-right:1px solid #CCCCCC; }
.scrolltable td,.scrollTable th{border-left: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; padding: 5px; }thead tr{position: relative;top: expression(this.parentElement.parentElement.parentElement.scrollTop);z-index:3;background-color:#CCCCCC;}--> </style><script src=
"http://code.jquery.com/jquery-1.11.0.min.js"
></script><script>$(
function
(){ $(
'#myt'
).wrap(
"<div id='scrollDiv' class='scrollDiv' >"
);})</script></head><body><h1>利用CSS代码让Table产生固定表头</h1><h3>www.865171.cn</h3><table><tr><td><table id=
"myt"
border=
"0"
cellpadding=
"3"
cellspacing=
"0"
width=
"300"
class
=
"scrollTable"
> <thead > <tr > <th >h1</th> <th >h2</th> <th >h3</th> <th >h4</th> <th >h5</th> </tr> </thead> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox"
value=
"checkbox"
> a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox2"
value=
"checkbox"
> b</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td nowrap
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox3"
value=
"checkbox"
> c</td> <td nowrap>单元格2</td> <td nowrap>单元格3</td> <td nowrap>单元格4</td> <td nowrap>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox4"
value=
"checkbox"
> d</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox5"
value=
"checkbox"
> e</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox6"
value=
"checkbox"
> f</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox7"
value=
"checkbox"
> g</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> </table> </td> </tr></table></body><script>//document.all.myt.insertAdjacentHTML(
"afterBegin"
,
"<h1>在文本前容器内插入内容</h1>"
);//document.all.myt.insertAdjacentHTML(
"beforeEnd"
,
"<h2>在文本后容器内插入内容</h2>"
);//document.all.myt.insertAdjacentHTML(
"beforeBegin"
,
"<h2>在文本后容器内插入内容</h2>"
);//document.all.myt.insertAdjacentHTML(
"beforeBegin"
,
"<div id='scrollDiv' class='scrollDiv' >"
);</script></html>
Copy after login
Copy after login
Use jquery’s warp method to outsource a div, as follows
1
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
><html xmlns=
"http://www.w3.org/1999/xhtml"
><head><meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/><title>无标题文档</title><style type=
"text/css"
> <!-- body,table, td, a {font:9pt;}
.scrollRowThead{position: relative; left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);z-index:0;}
.scrollCR { z-index:3;}
.scrollDiv {height:200px;clear: both; border: 1px solid #EEEEEE;OVERFLOW: scroll;width: 300px; }
.scrollColThead td,.scrollColThead th{ text-align: center ;}
.scrollRowThead,.scrollColThead td,.scrollColThead th{background-color:#FFFFFF;}
.scrolltable{border-bottom:1px solid #CCCCCC; border-right:1px solid #CCCCCC; }
.scrolltable td,.scrollTable th{border-left: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; padding: 5px; }thead tr{position: relative;top: expression(this.parentElement.parentElement.parentElement.scrollTop);z-index:3;background-color:#CCCCCC;}--> </style><script src=
"http://code.jquery.com/jquery-1.11.0.min.js"
></script><script>$(
function
(){ $(
'#myt'
).wrap(
"<div id='scrollDiv' class='scrollDiv' >"
);})</script></head><body><h1>利用CSS代码让Table产生固定表头</h1><h3>www.865171.cn</h3><table><tr><td><table id=
"myt"
border=
"0"
cellpadding=
"3"
cellspacing=
"0"
width=
"300"
class
=
"scrollTable"
> <thead > <tr > <th >h1</th> <th >h2</th> <th >h3</th> <th >h4</th> <th >h5</th> </tr> </thead> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox"
value=
"checkbox"
> a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox2"
value=
"checkbox"
> b</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td nowrap
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox3"
value=
"checkbox"
> c</td> <td nowrap>单元格2</td> <td nowrap>单元格3</td> <td nowrap>单元格4</td> <td nowrap>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox4"
value=
"checkbox"
> d</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox5"
value=
"checkbox"
> e</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox6"
value=
"checkbox"
> f</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox7"
value=
"checkbox"
> g</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> </table> </td> </tr></table></body><script>//document.all.myt.insertAdjacentHTML(
"afterBegin"
,
"<h1>在文本前容器内插入内容</h1>"
);//document.all.myt.insertAdjacentHTML(
"beforeEnd"
,
"<h2>在文本后容器内插入内容</h2>"
);//document.all.myt.insertAdjacentHTML(
"beforeBegin"
,
"<h2>在文本后容器内插入内容</h2>"
);//document.all.myt.insertAdjacentHTML(
"beforeBegin"
,
"<div id='scrollDiv' class='scrollDiv' >"
);</script></html>
Copy after login
Copy after login
Hello, after implementing the append, I want to have the effect of fixed header, can you help me? Change it?
1
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
><html xmlns=
"http://www.w3.org/1999/xhtml"
><head><meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/><title>无标题文档</title><style type=
"text/css"
> <!-- body,table, td, a {font:9pt;}
.scrollRowThead{position: relative; left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);z-index:0;}
.scrollCR { z-index:3;}
.scrollDiv {height:200px;clear: both; border: 1px solid #EEEEEE;OVERFLOW: scroll;width: 300px; }
.scrollColThead td,.scrollColThead th{ text-align: center ;}
.scrollRowThead,.scrollColThead td,.scrollColThead th{background-color:#FFFFFF;}
.scrolltable{border-bottom:1px solid #CCCCCC; border-right:1px solid #CCCCCC; }
.scrolltable td,.scrollTable th{border-left: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; padding: 5px; }thead tr{position: relative;top: expression(this.parentElement.parentElement.parentElement.scrollTop);z-index:3;background-color:#CCCCCC;}--> </style><script src=
"http://code.jquery.com/jquery-1.11.0.min.js"
></script><script>//+---------------------------------------------------//| jQuery插件:表头固定 (还需测试)//| author:阿明//+---------------------------------------------------(
function
($) { $.fn.extend({ ScrollTable:
function
(options) {
var
defaults = { MaxHeight: 120, MaxDataItemIndex: 1 // 固定前MaxDataItemIndex作为固定表头 }; options = $.extend(defaults, options);
return
this.each(
function
() {
var
$this
= $(this); // 产生表头部分和身体部分
var
$cloneTableHeader
=
$this
.
clone
();
var
$cloneTableBody
=
$this
.
clone
();
$cloneTableHeader
.find(
"tr"
).filter(
function
(index) {
return
index >= options.MaxDataItemIndex }).remove();
$cloneTableBody
.find(
"tr"
).filter(
function
(index) {
return
index < options.MaxDataItemIndex }).remove(); // 将产生的表头和身体部分放入Container,并做一些微调
$this
.after(
"<div class='ScrollTableContainer' style='border:1px solid;'></div>"
);
$this
.next().append(
$cloneTableHeader
);
$this
.next().append(
"<div class='ScrollDiv' style='overflow-y:scroll;'></div>"
);
$this
.next().css(
"width"
,
$this
.width() + 20);
$this
.next().find(
"div.ScrollDiv"
).css({
"max-height"
: options.MaxHeight,
"width"
:
$this
.width() + 20,
"margin-top"
: -2 });
$this
.next().find(
"div.ScrollDiv"
).append(
$cloneTableBody
);
$this
.remove(); }); } });})(jQuery);$(
function
(){ $(
'#myt'
).ScrollTable();})</script></head><body><h1>利用CSS代码让Table产生固定表头</h1><h3>www.865171.cn</h3><table><tr><td><table id=
"myt"
border=
"0"
cellpadding=
"3"
cellspacing=
"0"
width=
"300"
class
=
"scrollTable"
> <thead > <tr > <th >h1</th> <th >h2</th> <th >h3</th> <th >h4</th> <th >h5</th> </tr> </thead> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox"
value=
"checkbox"
> a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox2"
value=
"checkbox"
> b</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td nowrap
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox3"
value=
"checkbox"
> c</td> <td nowrap>单元格2</td> <td nowrap>单元格3</td> <td nowrap>单元格4</td> <td nowrap>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox4"
value=
"checkbox"
> d</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox5"
value=
"checkbox"
> e</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox6"
value=
"checkbox"
> f</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td
class
=
"scrollRowThead"
> <input type=
"checkbox"
name=
"checkbox7"
value=
"checkbox"
> g</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> </table> </td> </tr></table></body><script>//document.all.myt.insertAdjacentHTML(
"afterBegin"
,
"<h1>在文本前容器内插入内容</h1>"
);//document.all.myt.insertAdjacentHTML(
"beforeEnd"
,
"<h2>在文本后容器内插入内容</h2>"
);//document.all.myt.insertAdjacentHTML(
"beforeBegin"
,
"<h2>在文本后容器内插入内容</h2>"
);//document.all.myt.insertAdjacentHTML(
"beforeBegin"
,
"<div id='scrollDiv' class='scrollDiv' >"
);</script></html>
Copy after login
Actually this page is not what I want to use. I just want to use this interface to learn how to append DIVs and implement fixed table headers. Now I The situation is that the tables on the page are written by the platform using Java code, and are output to HTML when used. That is to say, the page has many tables, and one table contains the header and data. It seems that it doesn't matter if you put it in according to your code. Use it, I don’t know where the meter header is. ----------------------------------??????? ???????????????????????????????????????
_______________________________________________________________________________________ Could you please take a look at this page? ??????????????????????????????????????????? ???????
test <script> <br> / /------------------------------------------------- -- <br> //| jQuery plug-in: table header fixed (still needs to be tested) <br> //| author: A Ming <br> // ---------------- ---------------------------------- <br> (function ($) { <br> $.fn .extend({ <br> ScrollTable: function (options) { <br> var defaults = { <br> MaxHeight: 120, <br> MaxDataItemIndex : 1 // MaxDataItemIndex is used as the fixed header before fixing <br> }; <br> options = $.extend(defaults, options); <br> <br> return this.each(function () { <br> var $this = $(this); <br> <br> // Generate header Parts and body parts<br> var $cloneTableHeader = $this.clone(); var $cloneTableBody = $this.clone(); var $cloneTableHeader.find(" tr").filter(function (index ) { return index >= options.MaxDataItemIndex }).remove(); <br> $cloneTableBody.find("tr").filter(function (index) { return index < options.MaxDataItemIndex }).remove(); <br /> <br /> // 将产生的表头和身体部分放入Container,并做一些微调 <br /> $this.after("<div class='ScrollTableContainer' style='border:1px solid;'></div>"); <br> $this.next().append($cloneTableHeader); <br> $this.next().append("<div class='ScrollDiv' style='overflow-y:scroll;'></div>"); <br> $this.next().css("width", $this.width() + 20); <br> $this.next().find("div.ScrollDiv").css({ <br> "max-height": options.MaxHeight, <br> "width": $this.width() + 20, <br> "margin-top": -2 <br> }); <br> $this.next().find("div.ScrollDiv").append($cloneTableBody); <br> $this.remove(); <br> }); <br> } <br> }); <br> })(jQuery); <br> $(function(){ <br> $('#myt').ScrollTable(); <br> }) <br> </script>
?????????????????????????????????????????????????????? 不知道我改了什么,那个表头好像又出来了,现在就差表头和数据列对齐 总之还是谢谢你,学到了点东西,或许是我这情况太特殊了,我迟点就把分给你吧