admin管理员组文章数量:1305195
I am making a dynamic form using table. My table was like below.
<table border="1">
<tr>
<td>A Label Name</td>
<td>:</td>
<td colspan="4"><!-- Input text --></td>
</tr>
<tr>
<td>Category</td>
<td>:</td>
<td colspan="4"><!-- Select Option value: { both, group 1, group 2 } --></td>
</tr>
<tr>
<!-- Group 1 -->
<td>Group 1</td>
<td>:</td>
<td>- Group 1 Name -</td>
<!-- End of Group 1 -->
<!-- Group 2 -->
<td>Group 2</td>
<td>:</td>
<td>- Group 2 Name -</td>
<!-- End of Group 2 -->
</tr>
How to hide group 1 td element, so that the table will only display the td of group 2, without having to delete or clear that td elements by using javascript?
I have tried to add span tag to group them and then styling to display to bee none.
<span style="display:none">
<!-- Group 1 -->
<td>Group 1</td>
<td>:</td>
<td>- Group 1 Name -</td>
<!-- End of Group 1 -->
</span>
But nothing happened. Any idea?
I am making a dynamic form using table. My table was like below.
<table border="1">
<tr>
<td>A Label Name</td>
<td>:</td>
<td colspan="4"><!-- Input text --></td>
</tr>
<tr>
<td>Category</td>
<td>:</td>
<td colspan="4"><!-- Select Option value: { both, group 1, group 2 } --></td>
</tr>
<tr>
<!-- Group 1 -->
<td>Group 1</td>
<td>:</td>
<td>- Group 1 Name -</td>
<!-- End of Group 1 -->
<!-- Group 2 -->
<td>Group 2</td>
<td>:</td>
<td>- Group 2 Name -</td>
<!-- End of Group 2 -->
</tr>
How to hide group 1 td element, so that the table will only display the td of group 2, without having to delete or clear that td elements by using javascript?
I have tried to add span tag to group them and then styling to display to bee none.
<span style="display:none">
<!-- Group 1 -->
<td>Group 1</td>
<td>:</td>
<td>- Group 1 Name -</td>
<!-- End of Group 1 -->
</span>
But nothing happened. Any idea?
Share Improve this question edited Jun 13, 2017 at 11:07 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 24, 2010 at 8:45 Hafizul AmriHafizul Amri 2,6637 gold badges29 silver badges30 bronze badges 1- Assuming that you have just one form, <fieldset> is a more semantically appropriate element for grouping form inputs than <span>. This HTML is invalid though, and I am wondering why you use a table in the first place. – Mark Chorley Commented Oct 24, 2010 at 18:16
5 Answers
Reset to default 5I'm always wondering why people give jQuery solutions for simple problems.
I'm assuming here that you have a fixed format of your table, but you can easily adapt it if you need to hide more continuos cells:
Give your table an ID.
var table = document.getElementById('table-id');
var firstRow = table.rows[0];
var cells = firstRow.cells;
for(var i = 0, l = cells.length; i < 2 && i < l; i++) {
cells[i].style.display = 'none';
}
If you need to do this for every row, you can just loop over table.rows
.
With Jquery, it would be something like this:
NEW VERSION
http://jsfiddle/dactivo/VPe2U/
The most sofisticated way would be like this,slice gives you the chance to choose between first and second value (it's 0-based index).
$("#test tr:eq(0) td").slice(0,3).hide();
or in case you don't want to define the start point (as @Felix Kling ments):
$("#test tr:eq(0) td:lt(4)").hide()
OLD VERSION
http://jsfiddle/dactivo/SLzNT/
$("#test tr:eq(0) td:eq(0),#test tr:eq(0) td:eq(1),#test tr:eq(0) td:eq(2)").hide();
Which means hide the first/2nd/3rd cell in the first row
If you're using jQuery you can set a mon class on the tag (eg class="className") and use
$('.className').hide();
To initialise this you could try addind the style inline to the tag itself?
and use
$('.className').show();
to show the cells when you need them
You should make each group in it's own <tr>
, then you can hide one of the rows and not the other. I think you will get unexpected or undesired effects having 6 columns in a table with only three of them visible.
<table border="1">
<tr class="group1" style="display:none">
<!-- Group 1 -->
<td>Group 1</td>
<td>:</td>
<td>- Group 1 Name -</td>
<!-- End of Group 1 -->
</tr>
<tr class="group2">
<!-- Group 2 -->
<td>Group 2</td>
<td>:</td>
<td>- Group 2 Name -</td>
<!-- End of Group 2 -->
</tr>
...
And use javascript to set .group1
to display:block
and .group2
to display:none
.
[Working demo]
function hideCells(root, from, to) {
root = document.getElementById(root);
var all = root.getElementsByTagName("td");
if (to > all.length)
to = all.length;
for ( var i = from-1; i < to; i++ ) {
all[i].style.display = "none";
}
}
// USAGE
hideCells("table_id", 1, 3); // hides: 1, 2, 3 <td>
本文标签: javascriptHow to hide a group of td elementsStack Overflow
版权声明:本文标题:javascript - How to hide a group of td elements? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741774890a2396965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论