技术开发 频道

javascript 无限级树形菜单


【IT168技术文档】

1var list=new Array(); 2 list=[["","a0","aa1","target='blank' href='http://baidu.com/s?wd='"],["","a1","aa2","target='blank' href='http://baidu.com'"],["","a2","aa3","target='blank' href='http://baidu.com'"],["a0","2","bb","target='blank' href='http://baidu.com'"],["2","b2","bba","target='blank' href='http://baidu.com'"],["b2","b22","bbaaa","target='blank' href='http://baidu.com'"],["b2","b221","bbaaa","target='blank' href='http://baidu.com'"],["b2","b222","bbaaa","target='blank' href='http://baidu.com'"],["b2","b223","bbaaa","target='blank' href='http://baidu.com'"]]; 3 function CreateObjectNode(nodeName,id,className,text) 4 { 5 var obj=document.createElement(nodeName); 6 obj.id=id; 7 obj.className=className; 8 obj.innerHTML=text; 9 return obj; 10 } 11 12 function CreateItem(oMenuList,NodeName,BoxID) 13 { 14 var list=new Array(); 15 list=oMenuList; 16 var parentNode=document.getElementById(BoxID); 17 18 var len=parseInt(list.length); 19 for(i=0;i<len;i++) 20 { 21 22 if(list[i][0]==NodeName){ 23 var topBoxID=list[i][1]+i+"Top"; 24 var itemBoxID=list[i][1]+i+"Item"; 25 var titleBoxID=list[i][1]+i+"Title"; 26 27 //create top-box 28 if(!document.getElementById(titleBoxID)) 29 { 30 var item=CreateObjectNode("div",topBoxID,"","");} 31 32 //create title-box 33 if(!document.getElementById(titleBoxID)) 34 {var itemObj=CreateObjectNode("div",titleBoxID,"","-<a "+list[i][3]+" onclick=\"CreateSingleMenu('"+list[i][1]+"','"+itemBoxID+"');\">"+list[i][2]+"</a>"); 35 item.appendChild(itemObj);} 36 //create item-box 37 if(!document.getElementById(itemBoxID)) 38 { var itemObjItem=CreateObjectNode("div",itemBoxID,"",""); 39 //itemObj.onclick="CreateSingleMenu('"+list[i][1]+"','"+itemBoxID+"')"; 40 41 42 item.appendChild(itemObjItem);} 43 44 parentNode.appendChild(item); 45 } 46 } 47 } 48 function CreateSingleMenu(NodeName,BoxID) 49 { 50 var child=document.getElementById(BoxID).childNodes; 51 for(i=0;i<child.length;i++) 52 {child[i].innerHTML="";document.getElementById(BoxID).removeChild(child[i])} 53 if(NodeName=="") 54 {document.getElementById(BoxID).innerHTML="<a onclick=\"CreateSingleMenu('','box');\">clean</a>";} 55 CreateItem(list,NodeName,BoxID) 56 } 57 CreateSingleMenu("","box");
0
相关文章