首页 > 解决方案 > jquery设置表th的宽度等于td的宽度

问题描述

我在这里有一个 codepen - https://codepen.io/mt-ttmt/pen/OZRpdP

它是一个简单的表格,滚动时带有粘性标题

标头使用位置固定:固定;。这会从 th 中删除宽度

我想设置 th 的宽度,使它们与 td 的宽度相同

如何循环遍历 th 并将宽度设置为与 td 相同

$(function () {

    $(window).scroll(function () {

        var theadHeight = $(".js-table-thead").outerHeight();
        var tablePos = $(".js-table").offset().top;

        if ($(window).scrollTop() > tablePos) {
            let stickyPos = ($(this).scrollTop() - tablePos);
            $(".js-table-thead tr").addClass('fixed');

            var th = $('.js-table-thead th');
            var td = $('.js-table-tbody td');

            th.each(function(index){
              $(this).width() = td.width();        
            })


        } else {
            $(".js-table-thead tr").removeClass('fixed');
        }
    });
});

标签: jquery

解决方案


推荐阅读