js实现无限级树形导航列表

<html>
<head>
<title>js实现的无限级树形下拉导航列表 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
    *{ margin:0; padding:0;    list-style:none;}
    body { margin:20px;}
    h2 { font-family:"黑体"; font-size:24px; text-align:center; line-height:32px;}
    h5 { font-size:12px; text-align:center; font-weight:normal; color:#666; line-height:28px;}
    #nav a { text-decoration:underline;color:#06c; font-size:14px; line-height:24px;}
    #nav ul{ margin-bottom:5px;}
    #nav strong{ color:#696;}
    #nav.dyn li ul{ display:none;}
    #nav.dyn li ul.show{ display:block;}
    #nav.dyn li{ padding-left:15px;}
    #nav.dyn li.parent{    background:url(user_23.gif) 5px 10px no-repeat;}
    #nav.dyn li.open{ background:url(user_23.gif) 5px -34px no-repeat;}
    </style>
</head>
<body>
<script type="text/javascript">
    DOMhelp = {
        debugWindowId: ‘DOMhelpdebug‘,
        init: function () {
            if (!document.getElementById || !document.createTextNode) { return; }
        },
        lastSibling: function (node) {
            var tempObj = node.parentNode.lastChild;
            while (tempObj.nodeType != 1 && tempObj.previousSibling != null) {
                tempObj = tempObj.previousSibling;
            }
            return (tempObj.nodeType == 1) ? tempObj : false;
        },
        firstSibling: function (node) {
            var tempObj = node.parentNode.firstChild;
            while (tempObj.nodeType != 1 && tempObj.nextSibling != null) {
                tempObj = tempObj.nextSibling;
            }
            return (tempObj.nodeType == 1) ? tempObj : false;
        },
        getText: function (node) {
            if (!node.hasChildNodes()) { return false; }
            var reg = /^\s+$/;
            var tempObj = node.firstChild;
            while (tempObj.nodeType != 3 && tempObj.nextSibling != null || reg.test(tempObj.nodeValue)) {
                tempObj = tempObj.nextSibling;
            }
            return tempObj.nodeType == 3 ? tempObj.nodeValue : false;
        },
        setText: function (node, txt) {
            if (!node.hasChildNodes()) { return false; }
            var reg = /^\s+$/;
            var tempObj = node.firstChild;
            while (tempObj.nodeType != 3 && tempObj.nextSibling != null || reg.test(tempObj.nodeValue)) {
                tempObj = tempObj.nextSibling;
            }
            if (tempObj.nodeType == 3) { tempObj.nodeValue = txt } else { return false; }
        },
        createLink: function (to, txt) {
            var tempObj = document.createElement(‘a‘);
            tempObj.appendChild(document.createTextNode(txt));
            tempObj.setAttribute(‘href‘, to);
            return tempObj;
        },
        createTextElm: function (elm, txt) {
            var tempObj = document.createElement(elm);
            tempObj.appendChild(document.createTextNode(txt));
            return tempObj;
        },
        closestSibling: function (node, direction) {
            var tempObj;
            if (direction == -1 && node.previousSibling != null) {
                tempObj = node.previousSibling;
                while (tempObj.nodeType != 1 && tempObj.previousSibling != null) {
                    tempObj = tempObj.previousSibling;
                }
            } else if (direction == 1 && node.nextSibling != null) {
                tempObj = node.nextSibling;
                while (tempObj.nodeType != 1 && tempObj.nextSibling != null) {
                    tempObj = tempObj.nextSibling;
                }
            }
            return tempObj.nodeType == 1 ? tempObj : false;
        },
        initDebug: function () {
            if (DOMhelp.debug) { DOMhelp.stopDebug(); }
            DOMhelp.debug = document.createElement(‘div‘);
            DOMhelp.debug.setAttribute(‘id‘, DOMhelp.debugWindowId);
            document.body.insertBefore(DOMhelp.debug, document.body.firstChild);
        },
        setDebug: function (bug) {
            if (!DOMhelp.debug) { DOMhelp.initDebug(); }
            DOMhelp.debug.innerHTML += bug + ‘\n‘;
        },
        stopDebug: function () {
            if (DOMhelp.debug) {
                DOMhelp.debug.parentNode.removeChild(DOMhelp.debug);
                DOMhelp.debug = null;
            }
        },
        getKey: function (e) {
            if (window.event) {
                var key = window.event.keyCode;
            } else if (e) {
                var key = e.keyCode;
            }
            return key;
        },
        /* helper methods */
        getTarget: function (e) {
            var target = window.event ? window.event.srcElement : e ? e.target : null;
            if (!target) { return false; }
            while (target.nodeType != 1 && target.nodeName.toLowerCase() != ‘body‘) {
                target = target.parentNode;
            }
            return target;
        },
        stopBubble: function (e) {
            if (window.event && window.event.cancelBubble) {
                window.event.cancelBubble = true;
            }
            if (e && e.stopPropagation) {
                e.stopPropagation();
            }
        },
        stopDefault: function (e) {
            if (window.event && window.event.returnValue) {
                window.event.returnValue = false;
            }
            if (e && e.preventDefault) {
                e.preventDefault();
            }
        },
        cancelClick: function (e) {
            if (window.event) {
                window.event.cancelBubble = true;
                window.event.returnValue = false;
            }
            if (e && e.stopPropagation && e.preventDefault) {
                e.stopPropagation();
                e.preventDefault();
            }
        },
        addEvent: function (elm, evType, fn, useCapture) {
            if (elm.addEventListener) {
                elm.addEventListener(evType, fn, useCapture);
                return true;
            } else if (elm.attachEvent) {
                var r = elm.attachEvent(‘on‘ + evType, fn);
                return r;
            } else {
                elm[‘on‘ + evType] = fn;
            }
        },
        cssjs: function (a, o, c1, c2) {
            switch (a) {
                case ‘swap‘:
                    o.className = !DOMhelp.cssjs(‘check‘, o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
                    break;
                case ‘add‘:
                    if (!DOMhelp.cssjs(‘check‘, o, c1)) { o.className += o.className ? ‘ ‘ + c1 : c1; }
                    break;
                case ‘remove‘:
                    var rep = o.className.match(‘ ‘ + c1) ? ‘ ‘ + c1 : c1;
                    o.className = o.className.replace(rep, ‘‘);
                    break;
                case ‘check‘:
                    var found = false;
                    var temparray = o.className.split(‘ ‘);
                    for (var i = 0; i < temparray.length; i++) {
                        if (temparray[i] == c1) { found = true; }
                    }
                    return found;
                    break;
            }
        },
        safariClickFix: function () {
            return false;
        }
    }
    DOMhelp.addEvent(window, ‘load‘, DOMhelp.init, false);
</script>
<script type="text/javascript">
<!--
    sn = {
        dynamicClass: ‘dyn‘,
        showClass: ‘show‘,
        parentClass: ‘parent‘,
        openClass: ‘open‘,
        navID: ‘nav‘,
        init: function () {
            var triggerLink;
            if (!document.getElementById || !document.createTextNode) { return; }
            var nav = document.getElementById(sn.navID);
            if (!nav) { return; }
            DOMhelp.cssjs(‘add‘, nav, sn.dynamicClass);
            var nested = nav.getElementsByTagName(‘ul‘);
            for (var i = 0; i < nested.length; i++) {
                triggerLink = nested[i].parentNode.getElementsByTagName(‘a‘)[0];
                DOMhelp.cssjs(‘add‘, triggerLink.parentNode, sn.parentClass);
                DOMhelp.addEvent(triggerLink, ‘click‘, sn.changeSection, false);
                triggerLink.onclick = DOMhelp.safariClickFix;
                if (nested[i].parentNode.getElementsByTagName(‘strong‘).length > 0) {
                    DOMhelp.cssjs(‘add‘, triggerLink.parentNode, sn.openClass);
                    DOMhelp.cssjs(‘add‘, nested[i], sn.showClass);
                }
            }
        },
        changeSection: function (e) {
            var t = DOMhelp.getTarget(e);
            var firstList = t.parentNode.getElementsByTagName(‘ul‘)[0];
            if (DOMhelp.cssjs(‘check‘, firstList, sn.showClass)) {
                DOMhelp.cssjs(‘remove‘, firstList, sn.showClass)
                DOMhelp.cssjs(‘swap‘, t.parentNode, sn.openClass, sn.parentClass);
            } else {
                DOMhelp.cssjs(‘add‘, firstList, sn.showClass)
                DOMhelp.cssjs(‘swap‘, t.parentNode, sn.openClass, sn.parentClass);
            }
            DOMhelp.cancelClick(e);
        }
    }
    DOMhelp.addEvent(window, ‘load‘, sn.init, false);
-->
</script>
<h2>js实现无限级树形导航列表</h2>
<ul id="nav">
   
    <li><a href="#">产品目录</a>
        <ul>
            <li><a href="#">大类别一</a>
                <ul>
                    <li><a href="#">小类别一</a>
                        <ul>
                            <li><a href="#">次类别一</a></li>
                            <li><a href="#">次类别二</a></li>
                        </ul>
                    </li>
                    <li><a href="#">小类别二</a></li>
                </ul>
            </li>
            <li><a href="#">大类别二</a></li>
            <li><a href="#">大类别三</a>
                <ul>
                    <li><a href="#">小类别一</a></li>
                    <li><a href="#">小类别二</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">服务</a>
        <ul>
            <li><a href="#">大类别一</a>
               <ul>
                    <li><a href="#">小类别一</a></li>
                    <li><a href="#">小类别二</a></li>
                </ul>
            </li>
            <li><a href="#">大类别二</a>
                 <ul>
                    <li><a href="#">小类别一</a></li>
                    <li><a href="#">小类别二</a></li>
                </ul>
            </li>
            <li><a href="#">大类别三</a>
                 <ul>
                    <li><a href="#">小类别一</a></li>
                    <li><a href="#">小类别二</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
</body>
</html>

时间: 2024-11-08 20:32:43

js实现无限级树形导航列表的相关文章

利用多叉树实现Ext JS中的无限级树形菜单(一种构建多级有序树形结构JSON的方法)

一.问题研究的背景和意义 目前在Web应用程序开发领域,Ext JS框架已经逐渐被广泛使用,它是富客户端开发中出类拔萃的框架之一.在Ext的UI控件中,树形控件无疑是最为常用的控件之一,它用来实现树形结构的菜单.TreeNode用来实现静态的树形菜单,AsyncTreeNode用来实现动态的异步加载树形菜单,后者最为常用,它通过接收服务器端返回来的JSON格式的数据,动态生成树形菜单节点.动态生成树有两种思路:一种是一次性生成全部树节点,另一种是逐级加载树节点(利用AJAX,每次点击节点时查询下

实用js+css多级树形展开效果导航菜单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题

SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题 分类: SharePoint2012-01-16 14:24 1295人阅读 评论(1) 收藏 举报 sharepoint2010queryurllistborder SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题 1.实现效果如下: 点击各个节点进入相应的链接 2.测试列表: 3.

【JS 设计模式 】用组合模式来实现树形导航--代码结构思路分析(一)

树导航效果图: 组合模式的描述: 将对象组合成树形结构以表示"部分-整体"的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 我们把部分用Leaf表示, 把整体用Composite表示.组合模式是有一定规律的,在实现树导航的情况下,Composite需要包含一个以上Leaf,也可以包含一个以上Leaf和一个以Composite,为什么说要包含一个以上的,如果Composite不包含任何子child的话那么它就是Leaf,Leaf表示是最后一层结节. 树形导航代码片段:

【JS 设计模式 】用组合模式来实现树形导航--JS代码结构思路分析(二)

[JS 设计模式 ]用组合模式来实现树形导航--代码结构思路分析(一) 根据上一节中的HTML代码结构我们通过JS来渲染HTML代码,我们先提供一下JS的代码片段,这代码代码不是一个完整的代码是经过简化的.通过JS代码来分析如何组装HTML的 Composite类型的代码: function TreeComposite(id, name, total, level, last) { var root = document.createDocumentFragment(); var panel =

java--css+js做的树形菜单(完整版)

jsp页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":"

css+jquery导航列表

<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>导航列表</title> <script src="jquery-1.11.2.js"></script> <link href="jquery.autocompleter.css" rel="stylesheet"

jquery,tree无限级树形菜单+简单实用案例

jquery,tree无限级树形菜单+简单实用案例 我在项目中用到产品类别的树形.各种地方都要用. 我就封装起来,方便以后调用. 记录下来,希望给新手们提供帮助.要记得导入jquery.js  tree.js 哦 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri=&quo

JQUERY实现的小巧简洁的无限级树形菜单

JQUERY实现的小巧简洁的无限级树形菜单,可用于后台或前台侧栏菜单!兼容性也比较好. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <