javascript来更改表头的宽度



这是我的表,我想通过javascript/jquery更改所有的宽度。

<div class="dataTables_scrollHeadInner" >  
        <table class="table table-condensed table-bordered table-striped dataTable no-footer" >   
            <thead class="bg-gray-active1">  
                <tr role="row">  
                    <th class="sorting_disabled" >Serial</th>  
                    <th class="sorting_disabled" >dsad</th>  
                    <th class="sorting_disabled" >vg</th>  
                    <th class="sorting_disabled" >sdfg</th>  
                </tr>  
            </thead>  
        </table>  
    </div>   

我想要第一个<th>的宽度:53px
我想要第二个<th>:152px的宽度
我想要第三个<th>:500px的宽度
我想要第四个<th>:100px 的宽度

您可以使用jquery

$( "tr th:nth-child(1)" ).attr("width","53"); 
$( "tr th:nth-child(2)" ).attr("width","152"); 
$( "tr th:nth-child(3)" ).attr("width","500"); 
$( "tr th:nth-child(4)" ).attr("width","100"); 

它选择每个具有th作为子(n)的tr。n是其子的编号

.attr用于添加html属性。更多信息请参阅此处http://www.w3schools.com/jquery/html_attr.asp

最新更新