使用CSS或JavaScript隐藏表中的特定行
我有一个HTML表格的大纲,我有数据通过javascript从在线数据集中插入其中。单元格0是包含信息的第一列。该列中的某些行的名称将显示在单元格0中,而某些行的破折号将显示在单元格0中。我在javascript中为cell0设置了一个类来隐藏dash。如何使用CSS或JavaScript隐藏cell0等于破折号的所有表行 JSFIDLE示例可以更好地理解我需要什么。我需要隐藏破折号行: HTML表格:使用CSS或JavaScript隐藏表中的特定行,javascript,html,css,Javascript,Html,Css,我有一个HTML表格的大纲,我有数据通过javascript从在线数据集中插入其中。单元格0是包含信息的第一列。该列中的某些行的名称将显示在单元格0中,而某些行的破折号将显示在单元格0中。我在javascript中为cell0设置了一个类来隐藏dash。如何使用CSS或JavaScript隐藏cell0等于破折号的所有表行 JSFIDLE示例可以更好地理解我需要什么。我需要隐藏破折号行: HTML表格: <table class="table table-borderless" id="
<table class="table table-borderless" id="top25">
<tbody>
<tr>
<th>Customer</th>
<th>Sales</th>
<th>MGN $</th>
<th>MGN %</th>
</tr>
</tbody>
</table>
如果将单元格指定为class:hide-dash,则可以使用css更改可见性:
hide-dash {
visibility: hidden;
}
必须将整行的显示设置为“无”。因此,将类“hide dash”添加到您创建的行中
.hide-dash {
display: none;
}
<table>
<tr class='hide-dash'>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</table>
更新:
使用jQuery可以做到这一点:
$'.hide dash'.parent.hide 您可以使用类hide dash获取包含单元格的所有行并将其隐藏:
var columsHide = customerTable.getElementsByClassName('hide-dash');
Array.prototype.forEach.call(columsHide, function(el) {
el.parentElement.style.display = "none";
});
请为see ITI制作一个代码片段或JSFIDLE如果要隐藏该行,那么为什么不在该行上设置该类?现在您正在单元格上设置它…@CyrilBeeckman我添加了一个JSFIDLE示例,说明我的表的外观如果我想隐藏该列中的所有行,这将起作用…但是,我只需要隐藏列等于dashPlus的行,这将隐藏td,不是tr这行不通,因为我的HTML中只有一个表模板…实际的表行是通过在javascript文件中插入数据创建的。我不能在HTML中实际添加hide dash类,因为我可以放的只是表头,我不希望类影响表头。我很确定这需要通过javascript来完成,这是多么完美!谢谢你的帮助!不客气。这是一个快速解决方案,但确实有效。但不确定您需要td cell类名做什么。
var columsHide = customerTable.getElementsByClassName('hide-dash');
Array.prototype.forEach.call(columsHide, function(el) {
el.parentElement.style.display = "none";
});
var customerTable = document.getElementById("top25");
var tableRow = customerTable.insertRow();
var cell0 = tableRow.insertCell(0);
cell0.style.textAlign = "left";
cell0.innerHTML = row['account filter'];
cell0.className='hide-dash';
if (row['account filter'] == "-") tableRow.style.display="none";
var cell1 = tableRow.insertCell(1);
cell1.innerHTML = row['sales'];
var cell2 = tableRow.insertCell(2);
cell2.innerHTML = row['margin dollar'];
var cell3 = tableRow.insertCell(3);
cell3.innerHTML = row['margin percent'];