随着Internet/Intranet的日益普及,Web编程和网页制作已成为一种趋势。本文给读者介绍一种在网页中实现折叠菜单的编程技术,相信会给你的网页增色不少。所谓折叠菜单其实是一种动态显示的菜单,即在未进行菜单操作时,只显示主菜单,当选择某菜单项时,其下级子菜单动态地显示出来,选择完成后,又隐藏起来。 实现原理 想必大家对HTML的<DIV>标记已非常熟悉,我们利用其display属性即可让<DIV>标记的内容隐藏或显示,具体地做法是当display设为“none”时隐藏,设为“”时显示。如果我们已经用<DIV>标记了所有的菜单名称(包括子菜单),只要用ASP语言并结合Script脚本动态地控制相应的菜单选项显示或隐藏,即可实现折叠菜单。 现在的问题是如何在程序中加入菜单项名称,当然可以在网页中直接加入,但是如果改变菜单选项时,必须重新改动网页的控制代码,这种办法显然不够明智。本文采用的是将所有的菜单选项名称按一定格式另外存放在一个文本文件中,在网页载入时,ASP代码自动读取其内容,这样如果改变菜单选项,只要在此文件中作相应改动即可。 关于文件操作,我们用ASP内置的文件存取组件来完成,具体的用法见文后的程序代码。 菜单文本文件 本文约定菜单文本文件内容必须遵循以下规则:描述一个菜单选项的名称必须有三行内容(见下例);文件中不允许有空行;菜单选项名称前可以有空格,但必须用空格键(space)形成,不能用Tab键;文件的结尾用两行“*END*”来完成。 假设有如下的三级菜单: 操作系统软件 计算机软件---- 应用系统软件 工具软件----- PC TOOLS CuteFTP 在菜单文本中应为如下格式: 1------表示第1个主菜单名 计算机软件-------- 菜单名称(以下同) 3 若不为0,指定本菜单的子菜单个数;本例为3个 1*1 表示第1个主菜单的第1个子菜单(必须用*号) 操作系统软件 0 http://… --------- 若为0,表示该菜单项没有子菜单,后跟超链接URL 1*2---------- 表示第1个主菜单的第2个子菜单(以下同) 应用系统软件 0 http://… 1*3 工具软件 2 1*3*1-------- 第1个主菜单中第3个子菜单的第1个子菜单 PC TOOLS 0 http://… 程序代码: <HTML> <HEAD> <SCRIPT Language="VBScript"> '显示或隐藏子菜单 Sub disp_sub_menu(curid) dim ct,i,tmpid ct=document.all(curid).style.ct i=1 While i<=CInt(ct) tmpid=curid+"*"+cstr(i) If document.all(tmpid).style.display="none" Then document.all(tmpid).style.display="" Else document.all(tmpid).style.display="none" If CInt(document.all(tmpid).style.ct)>0 Then If document.all(tmpid+"*1").style.display="" Then disp_sub_menu(tmpid) '递归调用下级子菜单 End If End If End If i=i+1 Wend End Sub </SCRIPT></HEAD><BODY> <FONT color=red><H2 align="center">用ASP在网页中实现折叠式菜单示例</H2></FONT><HR> <% '----| 计算菜单id中的*个数 |----- Function spnum(str) dim tmpstr,m,t tmpstr=str m=InStr(str,"*") t=0 While m<>0 t=t+1 tmpstr=Mid(tmpstr,m+1) m=InStr(tmpstr,"*") Wend spnum=t End Function '-----| 向浏览器送出一个菜单选项 |----- Sub output_line(ct_flag,curid,txtname,ct,txtcolor) dim ptl,sp,dispval,tspace sp=spnum(curid) dispval="none" If sp=0 Then dispval="" tspace="" While sp>0 tspace=tspace+" " sp=sp-1 Wend If ct_flag=1 Then '该级菜单有子菜单,只用<DIV>标记 ptl="<div id="+chr(34)+curid+chr(34)+" style=" ptl=ptl+chr(34)+"color:"+txtcolor+";" ptl=ptl+" ct:"+ct+"; line-height:25px;" ptl=ptl+" cursor:hand;" ptl=ptl+" display:"+dispval+chr(34) ptl=ptl+" onclick="+chr(34)+"disp_sub_menu('"+curid+"')"+chr(34) ptl=ptl+">"+tspace+txtname+"</div>"+chr(13) Else '该级菜单是最低层菜单,用<DIV>和<A>标记 ptl="<div id="+chr(34)+curid+chr(34) ptl=ptl+" style="+chr(34)+"display:"+dispval+"; " ptl=ptl+"line-height:25px; color:"+txtcolor+"; ct:0"+chr(34)+">" ptl=ptl+tspace+"<A href="+chr(34)+ct+"?txt="+txtname+chr(34)+">"+txtname+"</A></div>"+chr(13) End If response.write ptl End Sub '----| 主控过程 |----- dim curid,txtname,ct,ct_flag,txtcolor set fs=createobject("SCRIPTING.FILESYSTEMOBJECT") menufile=replace(request.servervariables("path_translated"),"menu.asp","mfile.txt") set txtstr=fs.opentextfile(menufile) curid=txtstr.readline While curid<>"*END*" curid="y"+Trim(curid) '形成当前菜单项的id txtname=Trim(txtstr.readline) '读取菜单名称 ct=Trim(txtstr.readline) '读取该项菜单的子菜单个数 ct_flag=1 If Mid(ct,1,1)="0" Then ct_flag=0 ct=LTrim(Mid(ct,2)) End If txtcolor="black" Select case spnum(curid) case 1 txtcolor="red" '一级子菜单颜色 case 2 txtcolor="green" '二级子菜单颜色 case 3 txtcolor="blue" '三级子菜单颜色,可继续增加 End Select output_line ct_flag,curid,txtname,ct,cstr(txtcolor) curid=txtstr.readline Wend txtstr.close %> <HR></BODY></HTML> 本代码在Win98/PWS(Personal Web Server)下通过。
|