使用Javascript隐藏表列
我有下表:使用Javascript隐藏表列,javascript,html,html-table,hide,Javascript,Html,Html Table,Hide,我有下表: <table id="btt-ranges" cellspacing="0" cellpadding="0" border="0" <tbody> <tr> <th scope="col"> </th> <th id="Business" scope="col">Type of Business</th> &l
<table id="btt-ranges" cellspacing="0" cellpadding="0" border="0"
<tbody>
<tr>
<th scope="col"> </th>
<th id="Business" scope="col">Type of Business</th>
<th id="Ranges" scope="col"> Ranges</th>
<th scope="col">BTT</th>
</tr>
<tr>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
</tr>
<tr>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
</tr>
<tr>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
</tr>
</tbody>
</table>
您不需要为此使用javascript。您可以使用CSS选择器隐藏最后一列:
#btt-ranges tr td:last-child { display: none; }
编辑:刚刚意识到你特别需要用javascript来做。不确定是否有任何方法可以在不接触表格的情况下附加样式。此代码选择列的单元格(th和tds),然后隐藏它们():
您可以使用css选择器选择最后一个td,就像使用“td:last child”一样。您可以用布尔值替换一些表达式。例如,4>=0可以替换为true。4每次都大于(或等于)0。谢谢你的帮助和耐心,我对这一切都不熟悉:)是的,只有Javascript,但谢谢你的支持:)你的回答对我很有帮助。很高兴你发了。
#btt-ranges tr td:last-child { display: none; }
var lastColHeader = Array.prototype.slice.call(document.querySelectorAll('th:last-child', '#btt-ranges'), 0); // get the header cell
var lastColCells = Array.prototype.slice.call(document.querySelectorAll('td:last-child', '#btt-ranges'), 0).concat(lastColHeader); // get the column cells, and add header
lastColCells.forEach(function(cell) { // iterate and hide
cell.style.display = 'none';
});