js无限分级 树_js实现无限级树形导航列表效果代码

本文实例讲述了js实现无限级树形导航列表效果代码。分享给大家供大家参考。具体如下:

这是一款js实现无限级树形下拉导航菜单,简洁实用,用到一个已封装好的JS类,有用的大家借鉴一下。

运行效果截图如下:

在线演示地址如下:

具体代码如下:

*{ 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(images/user_23.gif) 5px 10px no-repeat;}

#nav.dyn li.open{ background:url(images/user_23.gif) 5px -34px no-repeat;}

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

if(temparray[i]==c1){found=true;}

}

return found;

break;

}

},

safariClickFix:function(){

return false;

}

}

DOMhelp.addEvent(window, 'load', DOMhelp.init, false);

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

希望本文所述对大家的JavaScript程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值