简体   繁体   中英

CSS to hide table and show a tr

I wanted to hide a table on load and show one of its children td based on class name. Is it possible to do it with CSS?

I tried to do it but I think its inheriting from table which is hidden by display:none;

You can't show a child of a hidden parent.

However you can hide all the other <td> -s and show just the one you need.

<style>
td { display: none }
td.shown { display: table-cell }
</style>

<table>
    <tr>
        <td> hidden </td>
        <td> hidden </td>
    </tr>
    <tr>
        <td class="shown"> shown </td>
        <td> hidden </td>
    </tr>
    <tr>
        <td> hidden </td>
        <td> hidden </td>
    </tr>
</table>

DEMO


UPDATE

Using JavaScript

var tds = document.getElementsByTagName("td");

for ( var i = 0, size = tds.length; i < size; i++ ) {
    if ( !hasClass( tds[i], "shown" ) ) {
        tds[i].style.display = "none";
    }
}

function hasClass(element, cls) {
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}

DEMO

hasClass() function

If you hide the table, all of the content within the table will be hidden.

You want to hide all the <tr> s and only show per class

tr{
  display:none;
}
tr.showInit{
  display:table-row;
}

You cannot show the child and hide a parent .. That is not possible.

Instead hide all the td's you don't want to show ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM