2009-12-04 94 views
0

我正在使用Stupid Fixed Header修复两个表的标题。为此,我使用以下脚本。如何在jQuery中做到这一点?

$(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。是否可以只写一次?

回答

5
$("#table1, #table2").fixedHeader ... 
+0

伟大的工作 – 2009-12-04 09:05:33

2

使用类选择器而不是id选择器。类似于

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

给这两个表的类名[tablefidedheaders]。

1

正如adamantium所说,按班级选择可能是一种更好的做法。如果你想选择多个ID,你可以这样做:

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