制作方法:
第一步:嵌入代码。把下面这段代码复制到<head></head>标签之间。
<style>
<!--
#foldheader{cursor:hand ; font-weight:bold ;
list-style-image:url(http://www.163design.net/a/y/fold.gif)}
#foldinglist{list-style-image:url(list.gif)}
//-->
</style>
<script language="JavaScript1.2">
<!--
/**
* Based on Folding Menu Tree
* Dynamic Drive (www.dynamicdrive.com)
* For full source code, installation instructions,
* 100's more DHTML scripts, and Terms Of
* Use, visit dynamicdrive.com
*
* Updated to support arbitrarily nested lists
* by Mark Quinn (mark@robocast.com) November 2nd 1998
*/
var head="display:''"
img1=new Image()
img1.src="http://www.163design.net/a/y/fold.gif"
img2=new Image()
img2.src="http://www.163design.net/a/y/open.gif"
function change(){
if(!document.all)
return
if (event.srcElement.id=="foldheader") {
var srcIndex = event.srcElement.sourceIndex
var nested = document.all[srcIndex+1]
if (nested.style.display=="none") {
nested.style.display=''
event.srcElement.style.listStyleImage="url(http://www.163design.net/a/y/open.gif)"
}
else {
nested.style.display="none"
event.srcElement.style.listStyleImage="url(http://www.163design.net/a/y/fold.gif)"
}
}
}
document.onclick=change
//-->
</script>
第二步:制作菜单。将代码插入到页面中要出现树形目录的地方。下面是一段代码范例:
<li id="foldheader">News
<ul id="foldinglist" style="display:none" style=&{head};>
CNN
ABC News
Vancouver Sun
<li id="foldheader">Games
<ul id="foldinglist" style="display:none" style=&{head};>
GameSpot
Happy Puppy
Game Center
<li id="foldheader">Software
<ul id="foldinglist" style="display:none" style=&{head};>
outer 1
outer 2
<li id="foldheader">Nested
<ul id="foldinglist" style="display:none" style=&{head};>
nested 1
nested 2
outer 3
outer 4
如果我们仔细分析一下的话不难发现制作列表的秘诀就是一个大的列表
<li id="foldheader">主目录名称
<ul id="foldinglist" style="display:none" style=&{head};>
<a href="对应地址 ">子目录名称
<a href="对应地址 ">子目录名称
<a href="对应地址 ">子目录名称
这样的好处是添加子菜单非常容易只要不断制作一些列表出来,并且加上连接就可以制作出下拉菜单了。如果要实现多级的子菜单也很容易只要将某项子菜单的id设置成为foldheader并且不加连接,后面的格式就如加灰部分一样和前面的代码是一样的,嵌套使用。
第三步:将一段代码复制到上面列表代码的下面。
<script language="JavaScript1.2">
<!--
/**
* Get cookie routine by Shelley Powers
* (shelley.powers@ne-dev.com)
*/
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
// if cookie exists
if (offset != -1) {
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
if (get_cookie(window.location.pathname) != ''){
var openresults=get_cookie(window.location.pathname).split(" ")
for (i=0 ; i < openresults.length ; i++){
foldinglist[openresults[i]].style.display=''
document.all[foldinglist[openresults[i]].sourceIndex -
1].style.listStyleImage="url(http://www.163design.net/a/y/open.gif)"
}
}
if (document.all){
var nodelength=foldinglist.length-1
var nodes=new Array(nodelength)
va