最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to hide a group of td elements? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

5 Answers 5

Reset to default 5

I'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>  ​
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>