如何在jQuery中执行此操作?

时间:2009-12-04 07:49:06

标签: jquery html

我正在使用Stupid Fixed Header来修复两个表的标头。为此,我使用以下脚本。

$(document).ready(function(){       
    $("#table1").fixedHeader({
        height: 160,
        adjustWidth: function(th){
        if($.browser.msie){
            return $(th).width()+10;
        }
            return $(th).width();
        }
    });

    $("#table2").fixedHeader({
        height: 160,
        adjustWidth: function(th){
        if($.browser.msie){
            return $(th).width()+10;
        }
            return $(th).width();
        }
    });
})

现在重点是,我正在写两次相同的代码。一次为table1,然后为table2。是否可以只写一次?

3 个答案:

答案 0 :(得分:5)

$("#table1, #table2").fixedHeader ...

答案 1 :(得分:2)

使用类选择器而不是id选择器。像

这样的东西
$(".tablefidedheaders").fixedHeader({
        height: 160,
        adjustWidth: function(th){
        if($.browser.msie){
                return $(th).width()+10;
        }
                return $(th).width();
        }
    });

为两个表提供类名[tablefidedheaders]。

答案 2 :(得分:1)

正如adamantium所说,按课程选择可能是一种更好的做法。如果要通过多个ID进行选择,可以执行以下操作:

$(document).ready(function(){           
    $("#table1, #table2").fixedHeader({
        height: 160,
        adjustWidth: function(th){
        if($.browser.msie){
                return $(th).width()+10;
        }
                return $(th).width();
        }
    });
相关问题