Javascript HTML表固定标题,最大列宽和使用jQuery填充

Javascript HTML表固定标题,最大列宽和使用jQuery填充,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个带有标题的HTML表,一旦用户向下滚动通过它,我就用jQuery将其锁定在浏览器的顶部。我在某某和其他地方看到了很多解决这个问题的方法,但我无法克服遇到的障碍 我已经能够让jQuery很好地锁定标题(我在中有一个包含我的列标题,并且我已经给了一个ID“theadatrid”) 如果我删除该样式,所有标题和它们的列都会完美地排列在一起。此样式是必需的,无法删除。表格宽度也需要设置为100%。有什么建议吗 JSFIDLE(如果删除最后2个CSS属性,您可以看到所需的行为,这个问题在Firef

我有一个带有标题的HTML表,一旦用户向下滚动通过它,我就用jQuery将其锁定在浏览器的顶部。我在某某和其他地方看到了很多解决这个问题的方法,但我无法克服遇到的障碍

我已经能够让jQuery很好地锁定标题(我在中有一个包含我的列标题,并且我已经给了一个ID“theadatrid”)

如果我删除该样式,所有标题和它们的列都会完美地排列在一起。此样式是必需的,无法删除。表格宽度也需要设置为100%。有什么建议吗

JSFIDLE(如果删除最后2个CSS属性,您可以看到所需的行为,这个问题在Firefox中最为明显):

试试这个(),我认为它可能是解决方案:

    <style>
    table {
        width: 100%;
    }
    td {
        background-color: #BCE6E6;    
    }
    #theadTrId {
        background-color: #6699FF;
    }
    #th1 {
        width: 110px;
    }
    #th2 {
        width: 235px;
    }
    #th3 {
        width: 80px;
    }
    td {
        max-width: 200px;
    }
    td, th {
        padding: 10px;
    }

    </style>
    </head>
    <table border="1">
        <thead>
            <tr id="theadTrId">
                <th id="th1">Column 1</th>
                <th id="th2">Column 2</th>
                <th id="th3">Column 3</th>
            </tr>
        </thead>

        <tbody id="tbody">

        </tbody>
    </table>
    <script>
    //loop to add 40 rows
for (var i=0;i < 41; i++)
{ 
    var jj = 1;
    $("#tbody").append("<tr><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td></tr>")
}

var widths = new Array();
//create array of <th> widths
var i = 0;
$('th').each(function () {
    widths[i] = $(this).context.offsetWidth;

    i++;
});

var elementPosTop = $('#theadTrId').offset().top;
$(window).scroll(function () {
    SetWidths();
});

var SetWidths = function () {
       var wintop = $(window).scrollTop();

       if (wintop > elementPosTop) {
           $('#theadTrId').css({ "position": "fixed", "top": "0" });
           //use the css rulz to change also the max-width for that particular td
           //it can be rolled back afther
           //<th> and <td> widths in 1st column
           $('#th1').css('width',widths[0]);
           $('#td1').css('width',widths[0]);
           $('#td1').css('max-width',widths[0]);
           //<th> and <td> widths in 2nd column
           $('#th2').css('width',widths[1]);
           $('#td2').css('width',widths[1]);
           $('#td2').css('max-width',widths[1]);
           //<th> and <td> widths in 3rd column
           $('#th3').css('width',widths[2]);
           $('#td3').css('width',widths[2]);
           $('#td3').css('max-width',widths[2]);

       } else {
           $('#theadTrId').css({ "position": "inherit" });
           // if you need the max-width again 
           $('#td0').css('max-width',200);
           $('#td1').css('max-width',200);
           $('#td2').css('max-width',200);
       }
   };
    </script>

桌子{
宽度:100%;
}
运输署{
背景色:#BCE6E6;
}
#泰德特里德{
背景色:#6699FF;
}
#th1{
宽度:110px;
}
#th2{
宽度:235px;
}
#th3{
宽度:80px;
}
运输署{
最大宽度:200px;
}
td,th{
填充:10px;
}
第1栏
第2栏
第3栏
//循环以添加40行
对于(变量i=0;i<41;i++)
{ 
var jj=1;
$(“#tbody”)。追加(“内容”+i+“内容”+i+“内容”+i+“内容”+i+”)
}
变量宽度=新数组();
//创建宽度数组
var i=0;
$('th')。每个(函数(){
宽度[i]=$(此).context.offsetWidth;
i++;
});
var elementPosTop=$('#theadatrid').offset().top;
$(窗口)。滚动(函数(){
设置宽度();
});
var SetWidths=函数(){
var wintop=$(window.scrollTop();
如果(wintop>elementPosTop){
$('theadatrid').css({“位置”:“固定”,“顶部”:“0”});
//使用css rulz还可以更改该特定td的最大宽度
//它可以在以后回滚
//第1列中的长度和宽度
$('#th1').css('width',widths[0]);
$('#td1').css('width',widths[0]);
$('#td1').css('max-width',widths[0]);
//第2列中的宽度
$('#th2').css('width',widths[1]);
$('#td2').css('width',widths[1]);
$('#td2').css('max-width',widths[1]);
//第3列中的宽度
$('#th3').css('width',widths[2]);
$('#td3').css('width',widths[2]);
$('#td3').css('max-width',widths[2]);
}否则{
$('#theadatrid').css({“position”:“inherit”});
//如果您再次需要最大宽度
$('#td0').css('max-width',200);
$('#td1').css('max-width',200);
$('#td2').css('max-width',200);
}
};

你能提供一个提琴或更多的代码吗,我不能理解使用或不使用那种风格的区别。当然,更新了问题,包括到我的JSFIDLE的链接
td {
    max-width: 200px;
}
td, th {
    padding: 10px;
}
    <style>
    table {
        width: 100%;
    }
    td {
        background-color: #BCE6E6;    
    }
    #theadTrId {
        background-color: #6699FF;
    }
    #th1 {
        width: 110px;
    }
    #th2 {
        width: 235px;
    }
    #th3 {
        width: 80px;
    }
    td {
        max-width: 200px;
    }
    td, th {
        padding: 10px;
    }

    </style>
    </head>
    <table border="1">
        <thead>
            <tr id="theadTrId">
                <th id="th1">Column 1</th>
                <th id="th2">Column 2</th>
                <th id="th3">Column 3</th>
            </tr>
        </thead>

        <tbody id="tbody">

        </tbody>
    </table>
    <script>
    //loop to add 40 rows
for (var i=0;i < 41; i++)
{ 
    var jj = 1;
    $("#tbody").append("<tr><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td></tr>")
}

var widths = new Array();
//create array of <th> widths
var i = 0;
$('th').each(function () {
    widths[i] = $(this).context.offsetWidth;

    i++;
});

var elementPosTop = $('#theadTrId').offset().top;
$(window).scroll(function () {
    SetWidths();
});

var SetWidths = function () {
       var wintop = $(window).scrollTop();

       if (wintop > elementPosTop) {
           $('#theadTrId').css({ "position": "fixed", "top": "0" });
           //use the css rulz to change also the max-width for that particular td
           //it can be rolled back afther
           //<th> and <td> widths in 1st column
           $('#th1').css('width',widths[0]);
           $('#td1').css('width',widths[0]);
           $('#td1').css('max-width',widths[0]);
           //<th> and <td> widths in 2nd column
           $('#th2').css('width',widths[1]);
           $('#td2').css('width',widths[1]);
           $('#td2').css('max-width',widths[1]);
           //<th> and <td> widths in 3rd column
           $('#th3').css('width',widths[2]);
           $('#td3').css('width',widths[2]);
           $('#td3').css('max-width',widths[2]);

       } else {
           $('#theadTrId').css({ "position": "inherit" });
           // if you need the max-width again 
           $('#td0').css('max-width',200);
           $('#td1').css('max-width',200);
           $('#td2').css('max-width',200);
       }
   };
    </script>