[英]How can I create a JS function that divides `width:100%` to the numbers of `th`?
I've got this scrollable table from a website and it's pretty amazing, but the problem is it doesn't resize the headers equally automatically plus due to lack of knowledge on JS I have no Idea on how to do it. 我从网站上获得了这个可滚动的表格,这非常令人惊讶,但是问题是它并没有自动地平等地调整标题的大小,而且由于缺乏对JS的了解,我不知道该怎么做。 I believe it's gonna use a
for
based on the numbers of tr > th
s then divide the 100% among them and set it as the width. 我相信它将基于
tr > th
s的数量使用for
,然后将其中的100%除以并将其设置为宽度。 Can you help me? 你能帮助我吗?
Beam me up Scotty! 向我微笑,斯科蒂!
PS: I want to fix it with JS, because I will make some changes in the future that will mess it up PS: 我想用JS修复它,因为将来我会做一些更改,将其弄乱
function removeClassName(elem, className) { elem.className = elem.className.replace(className, "").trim(); } function addCSSClass(elem, className) { removeClassName(elem, className); elem.className = (elem.className + " " + className).trim(); } String.prototype.trim = function() { return this.replace(/^\\s+|\\s+$/, ""); }; function stripedTable() { if (document.getElementById && document.getElementsByTagName) { var allTables = document.getElementsByTagName('table'); if (!allTables) { return; } for (var i = 0; i < allTables.length; i++) { if (allTables[i].className.match(/[\\w\\s ]*scrollTable[\\w\\s ]*/)) { var trs = allTables[i].getElementsByTagName("tr"); for (var j = 0; j < trs.length; j++) { removeClassName(trs[j], 'alternateRow'); addCSSClass(trs[j], 'normalRow'); } for (var k = 0; k < trs.length; k += 2) { removeClassName(trs[k], 'normalRow'); addCSSClass(trs[k], 'alternateRow'); } } } } } window.onload = function() { stripedTable(); };
th { word-break: break-all; } body { background: #FFF; color: #000; font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 10px; padding: 0 } table, td, a { color: #000; font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif } h1 { font: normal normal 18px Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 0 0 5px 0 } h2 { font: normal normal 16px Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 0 0 5px 0 } h3 { font: normal normal 13px Verdana, Geneva, Arial, Helvetica, sans-serif; color: #008000; margin: 0 0 15px 0 } /* end basic styling */ /* define height and width of scrollable area. Add 16px to width for scrollbar */ div.tableContainer { clear: both; border: 1px solid #963; height: 285px; overflow: auto; width: 100%; } /* Reset overflow value to hidden for all non-IE browsers. */ html>body div.tableContainer { overflow: hidden; width: 100%; height: 100%; } /* define width of the table. IE browsers only */ div.tableContainer table { float: left; width: 100%; } /* define width of table. Add 16px to width for scrollbar. */ /* All other non-IE browsers. */ html>body div.tableContainer table { width: 100%; } /* set table header to a fixed position. WinIE 6.x only */ /* In WinIE 6.x, any element with a position property set to relative and is a child of */ /* an element that has an overflow property set, the relative value translates into fixed. */ /* Ex: parent element DIV with a class of tableContainer has an overflow property set to auto */ thead.fixedHeader tr { position: relative } /* set THEAD element to have block level attributes. All other non-IE browsers */ /* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */ html>body thead.fixedHeader tr { display: block } /* make the TH elements pretty */ thead.fixedHeader th { background: #C96; border-left: 1px solid #EB8; border-right: 1px solid #B74; border-top: 1px solid #EB8; font-weight: normal; padding: 4px 3px; text-align: left } /* make the A elements pretty. makes for nice clickable headers */ thead.fixedHeader a, thead.fixedHeader a:link, thead.fixedHeader a:visited { color: #FFF; display: block; text-decoration: none; width: 100% } /* make the A elements pretty. makes for nice clickable headers */ /* WARNING: swapping the background on hover may cause problems in WinIE 6.x */ thead.fixedHeader a:hover { color: #FFF; display: block; text-decoration: underline; width: 100% } /* define the table content to be scrollable */ /* set TBODY element to have block level attributes. All other non-IE browsers */ /* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */ /* induced side effect is that child TDs no longer accept width: auto */ html>body tbody.scrollContent { display: block; height: 262px; overflow: auto; width: 100% } /* make TD elements pretty. Provide alternating classes for striping the table */ /* http://www.alistapart.com/articles/zebratables/ */ tbody.scrollContent td, tbody.scrollContent tr.normalRow td { background: #FFF; border-bottom: none; border-left: none; border-right: 1px solid #CCC; border-top: 1px solid #DDD; padding: 2px 3px 3px 4px } tbody.scrollContent tr.alternateRow td { background: #EEE; border-bottom: none; border-left: none; border-right: 1px solid #CCC; border-top: 1px solid #DDD; padding: 2px 3px 3px 4px } /* define width of TH elements: 1st, 2nd, and 3rd respectively. */ /* Add 16px to last TH for scrollbar padding. All other non-IE browsers. */ /* http://www.w3.org/TR/REC-CSS2/selector.html#adjacent-selectors */ /* define width of TD elements: 1st, 2nd, and 3rd respectively. */ /* All other non-IE browsers. */ /* http://www.w3.org/TR/REC-CSS2/selector.html#adjacent-selectors */ /*<----Cells*/ html>body tbody.scrollContent td { width: 100px; } html>body tbody.scrollContent td + td { width: 100px } html>body tbody.scrollContent td + td + td { width: 100px } /*<----Cells*/ /*<----Headers*/ html>body thead.fixedHeader th { width: 200px } html>body thead.fixedHeader th + th { width: 100px } html>body thead.fixedHeader th + th + th { width: 316px } /*<----Headers*/
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Pure CSS Scrollable Table with Fixed Header</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta http-equiv="language" content="en-us"> <style type="text/css"></style> </head> <body> <h1>Pure CSS Scrollable Table with Fixed Header</h1> <div id="tableContainer" class="tableContainer"> <table border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable"> <thead class="fixedHeader"> <tr class="alternateRow"> <th><a href="#">Header 1ahjsgdhjagsdhjgahjsdghjasgdhjagshjdgahjsdghjagsdhj</a> </th> <th><a href="#">Header 2</a> </th> <th><a href="#">Header 3</a> </th> </tr> </thead> <tbody class="scrollContent"> <tr class="normalRow"> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr class="alternateRow"> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> </tbody> </table> </div> <div> <br> </div> </body><span class="gr__tooltip"><span class="gr__tooltip-content"></span><i class="gr__tooltip-logo"></i><span class="gr__triangle"></span></span> </html>
What about using table-layout: fixed
? 关于使用
table-layout: fixed
呢?
table {
table-layout: fixed;
}
To find the width of the whole table use table.offsetWidth
, then cycle through your table like this: 要查找整个表格的宽度,请使用
table.offsetWidth
,然后像这样循环遍历表格:
var table = document.getElementById("your-table-id");
var w = table.offsetWidth; //total width of the table
for (var y = 0; y < table.rows.length; y++){ // cycle through rows
var row = table.rows[y];
for (var x = 0; x < row.cells.length; x++) { // cycle through cells
var cell = row.cells[x];
cell.style.width = (w / row.cells.length) + "px"; // add 'px' for a unit
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.