【问题标题】:TreeTable by HTML+CSSHTML+CSS 的 TreeTable
【发布时间】:2014-05-13 09:41:22
【问题描述】:

偶然发现了 TreeTable 的问题... 帮助理解,如何使第二级及更下级在勾选上td输入时不会出现。不幸的是,我只能使用 HTML+CSS。 how it looks like now on jsfiddle

<table class="table-tree" cellspacing="0">
        <tr>
            <td>
                <label for="folder2">Folder 1</label>
                <input type="checkbox" id="folder2" />
                <table class="table-wrapper">
                    <tr>
                        <td class="file"><a href="">Subfile 1</a></td>
                        <td class="file"><a href="">Subfile 2</a></td>
                        <td>
                            <label for="folder3">Subfolder 1</label>
                            <input type="checkbox" id="folder3" />
                            <table class="table-wrapper">
                                <tr>
                                    <td class="file"><a href="">Subsubfile 1</a></td>
                                    <td class="file"><a href="">Subsubfile 2</a></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

如果布局是这样就好了

有人可以帮帮我吗?

【问题讨论】:

  • 我认为这就是你要找的东西:thecssninja.com/css/css-tree-menu,不要使用 , 或 而使用
    1. 这将帮助你更容易理解和控制树
  • 您使用表格而不是 ul/li 是否有原因?
  • 你们都是对的!!!但是,如果我必须只使用 table、tr 和 td 而不是 ol 和 ul 和 li?这是用 table 做到这一点的一种方法吗?

标签: html css treetable


【解决方案1】:

您需要在 HTMLCSS 中将 table 替换为 ul(或 ol)和 li(删除 tr)替换 td em>,它看起来就像您图像上的散乱布局。

这样做的另一个原因是您显示的内容不是表格数据,它是文件列表,因此您不应使用表格,而应使用列表类型布局。

FIDDLE

HTML:

<ul class="table-tree" cellspacing="0">
    <li>
        <label for="folder2">Folder 1</label>
        <input type="checkbox" id="folder2" />
        <ul class="table-wrapper">
            <li class="file"><a href="">Subfile 1</a>

            </li>
            <li class="file"><a href="">Subfile 2</a>

            </li>
            <li>
                <label for="folder3">Subfolder 1</label>
                <input type="checkbox" id="folder3" />
                <ul class="table-wrapper">
                    <li class="file"><a href="">Subsubfile 1</a>

                    </li>
                    <li class="file"><a href="">Subsubfile 2</a>

                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

CSS:

*, html {
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
body, form, ul, li, p, h1, h2, h3, h4, h5 {
    margin: 0;
    padding: 0;
}
body {
    background-color: #fafafa;
    color: rgba(0, 0, 0, 0.7);
    margin: 0;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
/**
 * Tree Table CSS
**/
 .table-tree {
    display: block;
    padding: 30px 0 0 30px;
}
li {
    display: block;
    position: relative;
}
li label {
    background: url(http://www.webmasters.by/images/articles/css-tree/folder-horizontal.png) 15px 1px no-repeat;
    cursor: pointer;
    display: block;
    padding-left: 37px;
    width: 100%;
}
li input {
    position: absolute;
    left: 0;
    margin-left: 0;
    opacity: 0;
    z-index: 2;
    cursor: pointer;
    height: 1em;
    width: 1em;
    top: 0;
}
li input + ul {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small-expand.png) -3px -1px no-repeat;
    margin: -0.938em 0 0 0;
    height: 1em;
    width: 1em;
}
li input:checked + ul {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small.png) 41px 4px no-repeat;
    display: block;
    margin: -1.25em 0 0 -44px;
    padding: 1.25em 0 0 80px;
    height: auto;
    width: 100%;
}
li.file {
    margin-left: -1px !important;
}
li input + ul > li {
    display: none;
    margin-left: -14px !important;
    padding-left: 1px;
}
li input:checked + ul > li {
    display: block;
    margin: 0 0 0.125em;
}
li input:checked + ul > li:last-child {
    margin: 0 0 0.063em;
    /* 1px */
}

【讨论】:

  • 你们都是对的!!!但是,如果我必须只使用 table、tr 和 td 而不是 ol 和 ul 和 li?这是用 table 做到这一点的一种方法吗?
  • 保留表格会很麻烦...... ul/li 会简单得多。
【解决方案2】:

您的 CSS 有两个错误。

首先,您应该像这样选择未选中的复选框:

input:not(:checked)

并且不依赖于规则应用程序中的后备。

其次,更重要的是,td input:checked + table &gt; tbody td 的规则是错误的。

事实上,如果您选中了外部复选框但未选中嵌套复选框,则该规则仍将匹配嵌套表 td,因为第一部分 td input:checked + table &gt; tbody 匹配外部表主体,然后第二部分 td 查找 td将那个身体作为祖先

您应该改为检查直接子代。

总结一下,你可以把你的css改成这样:

*, html {
    font-family: Verdana, Arial, Helvetica, sans-serif;
}

body, form, ul, li, p, h1, h2, h3, h4, h5 {
    margin: 0;
    padding: 0;
}

body {
    background-color: #fafafa;
    color: rgba(0, 0, 0, 0.7);
    margin: 0;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

/**
 * Tree Table CSS
**/

.table-tree {
    display: block;
    padding: 30px 0 0 30px;
}

td {
    display: block;
    position: relative;
}

td label {
    background: url(http://www.webmasters.by/images/articles/css-tree/folder-horizontal.png) 15px 1px no-repeat;
    cursor: pointer;
    display: block;
    padding-left: 37px;
    width: 100%;
}

td input {
    position: absolute;
    left: 0;
    margin-left: 0;
    opacity: 0;
    z-index: 2;
    cursor: pointer;
    height: 1em;
    width: 1em;
    top: 0;
}
// CHANGED HERE
td input:not(:checked) + table {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small-expand.png) -3px -1px no-repeat;
    margin: -0.938em 0 0 0;
    height: 1em;
    width: 1em;
}

td input:checked + table {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small.png) 41px 4px no-repeat;
    display: block;
    margin: -1.25em 0 0 -44px;
    padding: 1.25em 0 0 80px;
    height: auto;
    width: 100%;
}

td.file {
    margin-left: -1px !important;
}
// CHANGED HERE
td input:not(:checked) + table > tbody > tr > td {
    display: none;
    margin-left: -14px !important;
    padding-left: 1px;
}
// CHANGED HERE
td input:checked + table > tbody > tr > td {
    display: block;
    margin: 0 0 0.125em;
}
// CHANGED HERE
td input:checked + table > tbody > tr > td:last-child {
    margin: 0 0 0.063em; /* 1px */
}

始终尽可能使用最具体的 CSS 规则,因为当 HTML 变得庞大时,您可能会遇到一些惊喜。

【讨论】:

【解决方案3】:

希望我的例子能帮到你demo

<ol class="tree">
        <li>
            <label for="folder1">Folder 1</label> <input type="checkbox" checked disabled id="folder1" /> 
            <ol>
                <li class="file"><a href="document.html.pdf">File 1</a></li>
                <li>
                    <label for="subfolder1">Subfolder 1</label> <input type="checkbox" id="subfolder1" /> 
                    <ol>
                        <li class="file"><a href="">Filey 1</a></li>
                        <li>
                            <label for="subsubfolder1">Subfolder 1</label> <input type="checkbox" id="subsubfolder1" /> 
                            <ol>
                                <li class="file"><a href="">File 1</a></li>
                                <li>
                                    <label for="subsubfolder2">Subfolder 1</label> <input type="checkbox" id="subsubfolder2" /> 
                                    <ol>
                                        <li class="file"><a href="">Subfile 1</a></li>
                                        <li class="file"><a href="">Subfile 2</a></li>
                                        <li class="file"><a href="">Subfile 3</a></li>
                                        <li class="file"><a href="">Subfile 4</a></li>
                                        <li class="file"><a href="">Subfile 5</a></li>
                                        <li class="file"><a href="">Subfile 6</a></li>
                                    </ol>
                                </li>
                            </ol>
                        </li>
                        <li class="file"><a href="">File 3</a></li>
                        <li class="file"><a href="">File 4</a></li>
                        <li class="file"><a href="">File 5</a></li>
                        <li class="file"><a href="">File 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder2">Folder 2</label> <input type="checkbox" id="folder2" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder2">Subfolder 1</label> <input type="checkbox" id="subfolder2" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder3">Folder 3</label> <input type="checkbox" id="folder3" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder3">Subfolder 1</label> <input type="checkbox" id="subfolder3" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder4">Folder 4</label> <input type="checkbox" id="folder4" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder4">Subfolder 1</label> <input type="checkbox" id="subfolder4" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder5">Folder 5</label> <input type="checkbox" id="folder5" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder5">Subfolder 1</label> <input type="checkbox" id="subfolder5" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
    </ol>

【讨论】:

  • 非常感谢您的回答。但是,不幸的是,我只能使用带有 tr 和 td 的表。 (((
  • 我明白,但仍然认为我的回答会有所帮助
猜你喜欢
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多