简体   繁体   English

如何使用Javascript或jQuery设置宽度 <th> 元素?

[英]How do I use Javascript or jQuery to set the width of <th> elements?

I have a website that is dynamically pulling information, setting the column widths in JavaScript. 我有一个网站正在动态提取信息,并在JavaScript中设置列宽。 I set the widths of the table body columns like this: 我像这样设置表主体列的宽度:

cell2.style.width="150px";
cell3.style.width="210px";
cell4.style.width="400px";
cell5.style.width="150px";

It works fine for setting up the body, but they do not line up with the table headers. 它可以很好地设置主体,但它们与表格标题不对齐。 The table headers are static and since the body is dynamic they are not lining up. 表头是静态的,并且由于主体是动态的,因此它们没有对齐。 Below is the static header: 以下是静态标头:

<th id="top1">Name</th>
<th id="top2">Type</th>
<th id="top3">Subject</th>
<th id="top4">Target(s)</th>

Fiddle Example 小提琴的例子

How do I give the same width values I have in the table body, to the <th> seen above and in my jsfiddle? 如何将表主体中的宽度值赋予上面和jsfiddle中的<th> I need to use JavaScript or jQuery, because it is a dynamic table and does not listen to my CSS commands, no matter what I add; 我需要使用JavaScript或jQuery,因为它是一个动态表,无论我添加什么,它都不会监听我的CSS命令。 not even !important . !important

You don't need jquery - just CSS - this will style even dynamic content. 您不需要jquery-仅需要CSS-甚至可以为动态内容设置样式。

 table { border-collapse: collapse; } table, th, td { border: 1px solid black; text-align:left } #tableTest th:nth-child(1), #tableTest td:nth-child(1) {width:150px} #tableTest th:nth-child(2), #tableTest td:nth-child(2) {width:210px} #tableTest th:nth-child(3), #tableTest td:nth-child(3) {width:400px} #tableTest th:nth-child(4), #tableTest td:nth-child(4) {width:150px} 
 <table id='tableTest'> <tr> <th id="top1">Name</th> <th id="top2">Type</th> <th id="top3">Subject</th> <th id="top4">Target(s)</th> </tr> <tr> <td id="cell1">1</td> <td id="cell2">2</td> <td id="cell3">3</td> <td id="cell4">4</td> </tr> </table> 

Table Header th has an attribute width which can be used for width. 表头具有可用于宽的属性宽度 If you want to do it with JS or JQuery. 如果您想用JS或JQuery做到这一点。 You can do it like 你可以像

document.getElementById('top1').setAttribute('width', '10%');

OR 要么

$('#top1').attr('width', '10%');

You can get the id selectors in jQuery and use the width() method to set the width of every th : 您可以在jQuery中获得id选择器,并使用width()方法设置每个th的宽度:

Updated FIDDLE . 更新了FIDDLE

$('#top1').width(100);
$('#top2').width(200);
$('#top3').width(300);
$('#top4').width(100);

Or using css() : 或使用css()

$('#top1').css('width', '100px');
$('#top2').css('width', '200px');
$('#top3').css('width', '300px');
$('#top4').css('width', '100px');

Or in JS you can use: 或者在JS中,您可以使用:

document.getElementById('#top1').style.width = '100px';
document.getElementById('#top2').style.width = '200px';
document.getElementById('#top3').style.width = '300px';
document.getElementById('#top4').style.width = '100px';

just use jQuery if you are using it. 只要使用jQuery,就可以使用它。

 var data = [{ name : "Test 1", type:"type1", subject:"subject1", targer:"target1" }, { name : "Test 2222222222", type:"type2", subject:"subject122222", targer:"target2" }, { name : "Test 3333333", type:"type13", subject:"subject3", targer:"target3333333" }]; $(document).ready(function(){ $('#top1').css('width','150px'); $('#top2').css('width','210px'); $('#top3').css('width','400px'); $('#top4').css('width','150px'); var table = $("#notificationMgmtTableBody"); data.forEach(function(item){ var tr = $("<tr>"); Object.keys(item).forEach(function(k){ var td = $("<td>"); td.text(item[k]); tr.append(td); }); table.append(tr); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="queryMgmt" style="height: 400px"> <table id="notificationMgmtTable" class="table table-bordered table-hover jupiterTable"> <thead id="notificationMgmtTableHead" style="width: 100%"> <tr style="background-color: rgba(204, 204, 204, .7); color: #003366; width: 100%"> <th id="top1">Name</th> <th id="top2">Type</th> <th id="top3">Subject</th> <th id="top4">Target(s)</th> </tr> </thead> <tbody id="notificationMgmtTableBody"> </tbody> </table> </div> 

Can you not just select the element by ID: 您能否仅按ID选择元素:

$('#top1').css('width','150px');

sorry my mistake.... it's early and the end of the week! 对不起,我的错了。。。

?

Working fiddle 工作提琴

Since you're using jQuery you could use .css() method : 由于您使用的是jQuery,因此可以使用.css()方法:

$('#top1').css('width','150px');
$('#top2').css('width','210px');
$('#top3').css('width','400px');
$('#top4').css('width','150px');

Hope this helps. 希望这可以帮助。

 $('#top1').css('width','150px'); $('#top2').css('width','210px'); $('#top3').css('width','400px'); $('#top4').css('width','150px'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="queryMgmt" style="height: 400px"> <table id="notificationMgmtTable" border=1 class="table table-bordered table-hover jupiterTable"> <thead id="notificationMgmtTableHead" style="width: 100%"> <tr style="background-color: rgba(204, 204, 204, .7); color: #003366; width: 100%"> <th id="top1">Name</th> <th id="top2">Type</th> <th id="top3">Subject</th> <th id="top4">Target(s)</th> </tr> </thead> <tbody id="notificationMgmtTableBody"> <tr> <td>Name 1</td> <td>Type 1</td> <td>Subject 1</td> <td>Target(s) 1</td> </tr> </tbody> </table> </div> 

To show a jQuery approach although as above - it is not required. 为了显示jQuery方法,尽管如上所述-并非必需。 You can set the desired widths into an array and then using jQuery - assign them to the th's and td's. 您可以将所需的宽度设置为数组,然后使用jQuery-将它们分配给th和td。

 $(document).ready(function(){ var widths = [150,210,400,150]; $('#tableTest th, #tableTest td').each(function(idx){ $(this).css('width',widths[idx]+'px'); }) }) 
 table { border-collapse: collapse; } table, th, td { border: 1px solid black; text-align:left } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='tableTest'> <tr> <th id="top1">Name</th> <th id="top2">Type</th> <th id="top3">Subject</th> <th id="top4">Target(s)</th> </tr> <tr> <td id="cell1">1</td> <td id="cell2">2</td> <td id="cell3">3</td> <td id="cell4">4</td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM