Javascript 如何使用纯CSS获取HTML中的树

Javascript 如何使用纯CSS获取HTML中的树,javascript,html,css,tree,Javascript,Html,Css,Tree,我正在努力遵循这一点,这里是到目前为止 本教程的末尾显示,分支中的最后一个节点后面不会有任何竖线。但我不能让它那样工作!。如果我做错了什么,或者教程遗漏了什么,你有什么想法吗 我甚至尝试了教程中所示的:last child伪类,但得到了相同的结果 遗憾的是,伪类是在即将发布的CSS3规范中定义的,目前很少有web浏览器支持它 它写在教程的末尾。也许这就是它不工作的原因。我相信我已经修复了它: 我修改了CSS以删除背景,并将边距改为填充 ul.tree, ul.tree ul { lis

我正在努力遵循这一点,这里是到目前为止

本教程的末尾显示,分支中的最后一个节点后面不会有任何竖线。但我不能让它那样工作!。如果我做错了什么,或者教程遗漏了什么,你有什么想法吗

我甚至尝试了教程中所示的:last child伪类,但得到了相同的结果

遗憾的是,伪类是在即将发布的CSS3规范中定义的,目前很少有web浏览器支持它

它写在教程的末尾。也许这就是它不工作的原因。

我相信我已经修复了它:

我修改了CSS以删除背景,并将边距改为填充

ul.tree, ul.tree ul {
    list-style-type: none;
    margin:0;
    padding:0;
}

ul.tree ul {
    padding-left: 1em;
    background:  url(vline.png) repeat-y;
}

ul.tree li {
    margin:0;
    padding: 0 1.2em;
    line-height: 20px;
    background: url(node.png) no-repeat;
    color: #369;
    font-weight: bold;
}

/* The #fff color background is to hide the previous background image*/
ul.tree li.last {
    background: #fff url(lastnode.png) no-repeat;
}

ul.tree ul:last-child {
    background: none;
}

以下是仅使用伪元素和边框的尝试:

ul, li{     
    position: relative;    
}

/* chop off overflowing lines */
ul{
    overflow: hidden;
}

li::before, li::after{
    content: '';
    position: absolute;
    left: 0;
}

/* horizontal line on inner list items */
li::before{
    border-top: 1px solid #333;
    top: 10px;
    width: 10px;
    height: 0;
}

/* vertical line on list items */    
li::after{
    border-left: 1px solid #333;
    height: 100%;
    width: 0px;
    top: -10px;
}

/* lower line on list items from the first level 
   because they don't have parents */
.tree > li::after{
    top: 10px;
}

/* hide line from the last of the first level list items */
.tree > li:last-child::after{
    display:none;
}

(已编辑)

谢谢大家,有用的答案让我读了更多,最后我阅读并删除了对JavaScript的所有依赖,并使用
nth-last-of-type
伪选择器将特殊背景应用于列表中的最后一个li项(忽略最后一个li之后的ul)

最后的代码是。我将接受我自己的答案,除非有人指出它有什么问题。(我认为在现阶段,与旧浏览器的兼容性对我来说并不重要。)

感谢@Aram的回答@一匹小马,你的回答超出了这个noob的范围:)我相信这是正确的,但对我来说有点太复杂了

<style type="text/css">
/* ul[class=tree] and every ul under it loses all alignment, and bullet
 * style.
 */
ul.tree, ul.tree ul {
    list-style-type: none;
    margin:0;
    padding:0;
}

/* Every ul under ul[class=tree] gets an indent of 1em, and a background
 * image (vertical line) applied to all nodes under it (repeat-y)
 */
ul.tree ul {
    padding-left: 1em;
    background:  url(vline.png) repeat-y;
}

/* ... except the last ul child in every ul; so no vertical lines for
 * the children of the last ul
  */
ul.tree ul:last-child {
    background: none;
}

/* Every li under ul[class=tree]
 *  - gets styling to make it bold and blue, and indented.
 *  - gets a background image (tilted T), to denote that its a node
 *  - sets height to match the height of background image
 */
ul.tree li {
    margin:0;
    padding: 0 1.2em;
    background: url(node.png) no-repeat;
    line-height: 20px;
    color: #369;
    font-weight: bold;
}

/*  The last li gets a different background image to denote it as the
 *  end of branch
 */
ul.tree li:nth-last-of-type(1) {
    background: url(lastnode.png) no-repeat;
}

/*ul[class=tree]及其下的每个ul失去所有对齐,并且
*风格。
*/
树,树,树{
列表样式类型:无;
保证金:0;
填充:0;
}
/*ul[class=tree]下的每个ul都有一个1em的缩进和一个背景
*应用于其下所有节点的图像(垂直线)(重复-y)
*/
树{
左:1米;
背景:url(vline.png)repeat-y;
}
/* ... 每个ul中的最后一个ul儿童除外;所以没有垂直线
*最后一代的孩子们
*/
最后一个孩子{
背景:无;
}
/*ul下的每一个li[类别=树]
*-获取样式以使其粗体、蓝色和缩进。
*-获取背景图像(倾斜的T),表示它是一个节点
*-设置高度以匹配背景图像的高度
*/
李树{
保证金:0;
填充:0.1.2米;
背景:url(node.png)不重复;
线高:20px;
颜色:#369;
字体大小:粗体;
}
/*最后一个li得到一个不同的背景图像,将其表示为
*分支结束
*/
ul.tree li:类型(1)的第n个最后一个{
背景:url(lastnode.png)不重复;
}

始终在问题本身中发布相关代码和标记,而不仅仅是链接。对您的演示进行了轻微的编辑。如果您注意到,当最后一个元素不在第一个或第二个缩进级别时(如现在所示),会有多余的行。如果您在这里的底部查看新的CSS,您可以看到它在结果中如何反映,而不需要额外的一行:@Joseph
margin left:0 0 10px
=>
margin:0 0 10px