優點:點擊body地區同樣可以隱藏下拉式功能表,且不會和body已有的onclick事件衝突。
缺點:請大家指出,虛心接受!
運行框代碼看似繁多,實屬無奈,請下載附件,以便更好觀察或修改~!
<!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=gb2312" /> <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:50px;width:200px;} .dropDownList span{display:block;width:146px;height:26px;background:url(yun_qi_img/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.parentNode,this); } liObj.onmouseover=function(){this.className="over";} liObj.onmouseout=function(){this.className="";} ulObj[0].appendChild(liObj); spanObj[0].onclick=function(event){ //如果當前是顯示的,就隱藏,反之亦然 showHiddenUl(this); } spanObj[0].onmouseover=function(){this.className='over';} spanObj[0].onmouseout=function(){this.className="";}; ulObj[0].onclick=function(){this.className="";} } parentContainer.onclick=function(event){ if(!event){event=window.event;} //阻止事件冒泡 event.cancelBubble=true; var eventUlObj=this.getElementsByTagName("ul")[0]; bodyClickHiddenUl(eventUlObj); } } } function selectCurrentItem(ulObj,currentObj){ var parentObj=ulObj.parentNode; var spanObj=parentObj.getElementsByTagName("span")[0]; spanObj.firstChild.nodeValue=currentObj.firstChild.nodeValue; var selectObj=parentObj.getElementsByTagName("select")[0]; selectObj.selectedIndex=parseInt(currentObj.getAttribute("currentIndex")); } function showHiddenUl(currentObj){ var parentNode=currentObj.parentNode; var ulObj=parentNode.getElementsByTagName("ul")[0]; if(ulObj.className==""){ ulObj.className="show"; }else{ ulObj.className=""; } } //點擊body地區(非"下拉式功能表")隱藏菜單 function addBodyClick(func) { var bodyObj=document.getElementsByTagName("body")[0]; var oldBodyClick = bodyObj.onclick; if (typeof bodyObj.onclick != 'function') { bodyObj.onclick = func; } else { bodyObj.onclick = function() { oldBodyClick(); func(); } } } //隱藏所有的UL function bodyClickHiddenUl(eventUlObj){ var existArray=____configArray.split("|"); for(var i=0;i<existArray.length;i++){ if(existArray[i].length<1){return;} //尋找所有UL並且隱藏 var parentContainer=document.getElementById(existArray[i]); if(!parentContainer){return;} var ulObj=parentContainer.getElementsByTagName("ul"); if(eventUlObj!=ulObj[0]){ ulObj[0].className=""; } } } var __dropDownList="dropDownList1|dropDownList2|dropDownList3"; __initDropDownList(__dropDownList); //添加這個可以確保點擊body地區的時候 也可以隱藏菜單 addBodyClick(bodyClickHiddenUl); </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]