日期:2014-05-18 浏览次数:20171 次
以下是HTML网页特效代码,点击运行按钮可查看效果: 以下是程序代码<!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-Type" content="text/html; charset=utf-8" /> <title>下拉菜单</title> <style type="text/css"> body{margin:20px auto;font-family:Arial, Helvetica, sans-serif;font-size:12px;width:950px;height:400px;border:solid 1px #aaa; position:relative;padding:10px;} ul{margin:0;padding:0;list-style:none;} .dropDownList{position:absolute;left:100px;top:100px;} /* .dropDownList div.dropdown select{display:none;}*/ .dropDownList div.dropdown{float:left;margin-right:120px;width:146px;} .dropDownList span{display:block;width:146px;height:26px;background:url(http://3.bp.blogspot.com/_bdBPvgEipxw/SRMUkLCdX0I/AAAAAAAAAP8/HgDXLapwpAw/s320/select.gif) left top no-repeat;line-height:26px;text-indent:12px;border:solid 1px #83BBD9;cursor:default;} .dropDownList span.over{background-position:left bottom;border-color:#B4C91A;} .dropDownList ul{background:#eee;width:200px;display:none;} .dropDownList ul li{height:20px;width:100%;padding:3px 0;text-indent:12px;cursor:default;} .dropDownList ul li.over{background:#ccc;} .dropDownList ul.show{display:block;} </style> </head> <body> <h1>JavaScript模拟下拉菜单,做的仓促,虚心接受批评。</h1> <form action="#" method="get"> <div class="dropDownList"> <div id="dropDownList1" class="dropdown"> <select name="birthday"> <option>请选择</option> <option>1988</option> <option>1989</option> </select> <span>请选择年份</span> <ul></ul> </div> <div id="dropDownList2" class="dropdown"> <select name="sex"> <option>性别</option> <option>男</option> <option>女</option> </select> <span>请选择性别</span> <ul></ul> </div> <div id="dropDownList3" class="dropdown"> <select name="student"> <option>幼儿班</option> <option>小学</option> <option>初中</option> </select> <span>请选择学历</span> <ul></ul> </div> </div> </form> </body> </html> <script type="text/javascript"> // JavaScript Document var ____configArray; function __initDropDownList(configArray){ //获取Select菜单 ____configArray=configArray; var existArray=configArray.split("|"); for(var i=0;i<existArray.length;i++){ if(existArray[i].length<1){return;} //根据参数分别获取div,并分别添加事件 var parentContainer=document.getElementById(existArray[i]); if(!parentContainer){return;} //获取下面的select,且获取其中的option var selectObj=parentContainer.getElementsByTagName("select"); if(selectObj.length<1){return;} var optionArray=selectObj[0].getElementsByTagName("option"); //获取option,并分别添加到各个li var optionLength=optionArray.length; for(var j=0;j<optionLength;j++){ //获取ul,以便能够添加项目 var ulObj=parentContainer.getElementsByTagName("ul"); if(ulObj.length<1){return;} //获取span,以便能显示当前选择的项目 var spanObj=parentContainer.getElementsByTagName("span"); if(spanObj.length<1){return;} var liObj=document.createElement("li"); var textNode=document.createTextNode(optionArray[j].firstChild.nodeValue) liObj.appendChild(textNode); liObj.setAttribute("currentIndex",j); //给每个liObj添加事件 liObj.onclick=function(){ selectCurrentItem(this.p