简体   繁体   English

什么是实现树视图的最佳方法?

[英]what is the best approach to implement Tree View?

I'm working Tree View constructed by using nested ul li tag as below: 我正在通过使用嵌套的ul li标签构造树视图,如下所示:

    <ul>
        <li>Level 1 a
            <ul>
                <li>Level 2 a</li>
                <li>Level 2 b
                    <ul>
                        <li>Level 3 a</li>
                        <li>Level 3 b</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>Level 1 b</li>
    </ul>

I wanted the list item is clickable on cell when navigate across the Tree View like below: 我希望在树状视图中导航时,列表项在单元格上可单击,如下所示:

在此处输入图片说明

I know that we can added JavaScript function on list item as below: 我知道我们可以在列表项上添加JavaScript函数,如下所示:

 <li onClick="redicrectPage(url)">

and add event.cancelBubble = true to avoid parent event is trigger when child item clicked. 并添加event.cancelBubble = true以避免单击子项时触发父事件。

My question is, any better cross-browser workaround on the implementation above? 我的问题是,以上实现是否有更好的跨浏览器解决方法?

Thank you in advanced. 在此先感谢您。

You can make a nested menu structure in CSS alone which would remove the bubbling problem. 您可以单独在CSS中创建嵌套菜单结构,这将消除冒泡问题。 The example at http://jsfiddle.net/steveukx/HfDBA/ uses the direct descendent selector to be able to repeat the same selectors without needing to know the depth of the menu, but if you are supporting browsers that don't have this functionality you should change the HTML to add classes to name the depth in the tree and specify those in the CSS. http://jsfiddle.net/steveukx/HfDBA/上的示例使用直接后代选择器可以重复相同的选择器,而无需知道菜单的深度,但是如果您支持的浏览器不具有此功能功能,您应该更改HTML以添加类以在树中命名深度并在CSS中指定深度。

HTML: HTML:

<ul class="menu">
    <li><a href="#">Level 1 a</a>
        <ul>
            <li><a href="#">Level 2 a</a></li>
            <li><a href="#">Level 2 b</a>
                <ul>
                    <li><a href="#">Level 3 a</a></li>
                    <li><a href="#">Level 3 b</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">Level 1 b</a></li>
</ul>​

CSS 的CSS

* { font-family: tahoma, sans-serif; font-size: 10pt; }
a { text-decoration: none; color: #fff; display: block; }
ul { display: none; }
ul.menu, li:hover > ul { display: block; }
li > ul { position: absolute; top: 25%; left: 100%; margin-left: -1em; 
   box-shadow: -1px 1px 1px rgba(0,0,0,0.5); z-index: 1000; }
li { position: relative; padding: 0.1em 0.5em; width: 100px; background: silver;
   box-shadow: -1px 1px 1px rgba(0,0,0,0.5); margin: 1px 1px 0; }
li:hover { background-color: #333; }
li:hover > a { color: #FAFAFE; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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