参考代码
代码示例1:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>原生js点击弹出多个城市选择框代码 - 站长素材</title> 6 <style type="text/css"> 7 body{padding-top:50px;font-size:12px;} 8 h2{margin:0px;padding:0px;font-size:12px;font-weight:bold;} 9 .bton{border:1px solid #CCC;background:#DDD;} 10 .cont{padding:10px;} 11 #main{400px;margin:0px auto;} 12 #selectItem{background:#FFF;position:absolute;top:0px;left:center;border:1px solid #000;overflow:hidden;margin-top:10px;400px;z-index:2;} 13 #preview{margin:1px;border:1px solid #CCC;} 14 #result{border:1px solid #CCC;margin-top:10px;} 15 .tit{line-height:20px;height:20px;margin:1px;padding-left:10px;} 16 .bgc_ccc{background:#CCC;} 17 .bgc_eee{background:#eee;} 18 .c_999{color:#999} 19 .pointer{cursor:pointer;} 20 .left{float:left;} 21 .right{float:right;} 22 .cls{clear:both;font-size:0px;height:0px;overflow:hidden;} 23 #bg{background:#CCC;filter:alpha(opacity=70);opacity:0.7;100%;;position:absolute;left:0px;top:0px;display:none;z-index:1;} 24 .hidden{display:none;} 25 .move{cursor:move;} 26 </style> 27 </head> 28 <body> 29 30 <div id="main"> 31 <input name="button" type="button" class="bton pointer" value="请选择" onclick="openBg(1);openSelect(1)"/> 32 <div id="result"> 33 <div class="tit bgc_eee"> 34 <h2>您已选择的城市汇总</h2> 35 </div> 36 <div class="cont" id="makeSureItem"></div> 37 </div> 38 </div> 39 40 <div id="bg"></div> 41 42 <div id="selectItem" class="hidden"> 43 <div class="tit bgc_ccc move" onmousedown="drag(event,this)"> 44 <h2 class="left">请选择城市</h2> 45 <span class="pointer right" onclick="openBg(0);openSelect(0);">[取消]</span> 46 <span class="pointer right" onclick="makeSure();">[确定]</span> 47 </div> 48 49 <div class="cls"></div> 50 51 <div class="cont"> 52 <div id="selectSub"> 53 <select name="" onchange="showSelect(this.value)" style="margin-bottom:10px;"> 54 <option value="0">第0层</option> 55 <option value="1">第1层</option> 56 <option value="2">第2层</option> 57 <option value="3">第3层</option> 58 </select> 59 <div id="c00"> 60 <input type="checkbox" name="ck00" onclick="addPreItem()" value="北京"/>北京 61 <input type="checkbox" name="ck00" onclick="addPreItem()" value="福建"/>福建 62 <input type="checkbox" name="ck00" onclick="addPreItem()" value="四川"/>四川 63 <input type="checkbox" name="ck00" onclick="addPreItem()" value="江苏"/>江苏 64 </div> 65 <div id="c01"> 66 <input type="checkbox" name="ck01" onclick="addPreItem()" value="上海"/>上海 67 <input type="checkbox" name="ck01" onclick="addPreItem()" value="云南"/>云南 68 <input type="checkbox" name="ck01" onclick="addPreItem()" value="贵州"/>贵州 69 </div> 70 <div id="c02"> 71 <input type="checkbox" name="ck01" onclick="addPreItem()" value="黑龙江"/>黑龙江 72 <input type="checkbox" name="ck01" onclick="addPreItem()" value="吉林"/>吉林 73 <input type="checkbox" name="ck01" onclick="addPreItem()" value="辽宁"/>辽宁 74 </div> 75 <div id="c03"> 76 <input type="checkbox" name="ck01" onclick="addPreItem()" value="美国"/>美国 77 <input type="checkbox" name="ck01" onclick="addPreItem()" value="阿富汗"/>阿富汗 78 <input type="checkbox" name="ck01" onclick="addPreItem()" value="日本"/>日本 79 </div> 80 </div> 81 </div> 82 83 <div id="preview"> 84 <div class="tit bgc_eee c_999"> 85 <h2>您已选择的城市</h2> 86 </div> 87 <div class="cont" id="previewItem"></div> 88 </div> 89 90 </div> 91 92 <div style="height:500px;"></div> 93 94 <script type="text/javascript"> 95 /* ------使用说明----- */ 96 /* 97 添加城市方法: 98 添加组:找到id 是 "selectSub"中select标签下,添加option标签 value属性递增,找到 id 是 "selectSub",按照原有格式添加div,其id属性递增 99 添加二级傅选矿选项 100 复制 id 是 "selectSub" 下任意input标签,粘贴在需要添加的位置。 101 */ 102 var grow = $("selectSub").getElementsByTagName("option").length; //组数 103 var showGrow = 0;//已打开组 104 var selectCount = 0; //已选数量 105 showSelect(showGrow); 106 var items = $("selectSub").getElementsByTagName("input"); 107 //alert(maxItem); 108 //var lenMax = 2; 109 //alert(1); 110 function $(o){ //获取对象 111 if(typeof(o) == "string") 112 return document.getElementById(o); 113 return o; 114 } 115 function openBg(state){ //遮照打开关闭控制 116 if(state == 1) 117 { 118 $("bg").style.display = "block"; 119 var h = document.body.offsetHeight > document.documentElement.offsetHeight ? document.body.offsetHeight : document.documentElement.offsetHeight; 120 //alert(document.body.offsetHeight); 121 //alert(document.documentElement.offsetHeight); 122 $("bg").style.height = h + "px"; 123 } 124 else 125 { 126 $("bg").style.display = "none"; 127 } 128 } 129 function openSelect(state){ //选择城市层关闭打开控制 130 if(state == 1) 131 { 132 $("selectItem").style.display = "block"; 133 $("selectItem").style.left = ($("bg").offsetWidth - $("selectItem").offsetWidth)/2 + "px"; 134 $("selectItem").style.top = document.body.scrollTop + 100 + "px"; 135 } 136 else 137 { 138 $("selectItem").style.display = "none"; 139 } 140 } 141 function showSelect(id){ 142 for(var i = 0 ; i < grow ;i++) 143 { 144 $("c0" + i).style.display = "none"; 145 } 146 $("c0" + id).style.display = "block"; 147 showGrow = id; 148 } 149 function open(id,state){ //显示隐藏控制 150 if(state == 1) 151 $(id).style.display = "block"; 152 $(id).style.diaplay = "none"; 153 } 154 function addPreItem(){ 155 $("previewItem").innerHTML = ""; 156 var len = 0 ; 157 for(var i = 0 ; i < items.length ; i++) 158 { 159 if(items[i].checked == true) 160 { 161 //len++; 162 //if(len > lenMax) 163 //{ 164 // alert("不能超过" + lenMax +"个选项!") 165 // return false; 166 //} 167 var mes = "<input type='checkbox' checked='true' value='"+ items[i].value +"' onclick='copyItem("previewItem","previewItem");same(this);'>" + items[i].value; 168 $("previewItem").innerHTML += mes; 169 //alert(items[i].value); 170 } 171 } 172 } 173 function makeSure(){ 174 //alert(1); 175 //$("makeSureItem").innerHTML = $("previewItem").innerHTML; 176 openBg(0); 177 openSelect(0); 178 copyItem("previewItem","makeSureItem") 179 } 180 function copyHTML(id1,id2){ 181 $(id2).innerHTML = $("id1").innerHTML; 182 } 183 function copyItem(id1,id2){ 184 185 var mes = ""; 186 var items2 = $(id1).getElementsByTagName("input"); 187 for(var i = 0 ; i < items2.length ; i++) 188 { 189 if(items2[i].checked == true) 190 { 191 mes += "<input type='checkbox' checked='true' value='"+ items2[i].value +"' onclick='copyItem("" + id2+ "",""+ id1 +"");same(this);'>" + items2[i].value; 192 } 193 } 194 $(id2).innerHTML = ""; 195 $(id2).innerHTML += mes; 196 //alert($(id2).innerHTML); 197 } 198 function same(ck){ 199 for(var i = 0 ; i < items.length ; i++) 200 { 201 if(ck.value == items[i].value) 202 { 203 items[i].checked = ck.checked; 204 } 205 } 206 } 207 /* 鼠标拖动 */ 208 var oDrag = ""; 209 var ox,oy,nx,ny,dy,dx; 210 function drag(e,o){ 211 var e = e ? e : event; 212 var mouseD = document.all ? 1 : 0; 213 if(e.button == mouseD) 214 { 215 oDrag = o.parentNode; 216 //alert(oDrag.id); 217 ox = e.clientX; 218 oy = e.clientY; 219 } 220 } 221 function dragPro(e){ 222 if(oDrag != "") 223 { 224 var e = e ? e : event; 225 //$(oDrag).style.left = $(oDrag).offsetLeft + "px"; 226 //$(oDrag).style.top = $(oDrag).offsetTop + "px"; 227 dx = parseInt($(oDrag).style.left); 228 dy = parseInt($(oDrag).style.top); 229 //dx = $(oDrag).offsetLeft; 230 //dy = $(oDrag).offsetTop; 231 nx = e.clientX; 232 ny = e.clientY; 233 $(oDrag).style.left = (dx + ( nx - ox )) + "px"; 234 $(oDrag).style.top = (dy + ( ny - oy )) + "px"; 235 ox = nx; 236 oy = ny; 237 } 238 } 239 document.onmouseup = function(){oDrag = "";} 240 document.onmousemove = function(event){dragPro(event);} 241 </script> 242 243 <div style="text-align:center;margin:0px 0; font:normal 14px/24px 'MicroSoft YaHei';"> 244 <p>适用浏览器:IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗. </p> 245 <p>来源:<a href="http://sc.chinaz.com/" target="_blank">站长素材</a></p> 246 </div> 247 248 </body> 249 </html>
效果:
代码示例2:
参考代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>下拉框</title> 6 <style type="text/css"> 7 .tab{width:500px;height:300px;border:1px solid #CCF ;} 8 .menu{width:480px;padding-top:50px;padding-left:50px;} 9 select{width:200px;height:50px;} 10 ul{padding:0;margin:0;} 11 .list{width:320px;height:80px;;border:1px solid #CCC;margin:5px 0 0 138px;padding:10px 20px 10px 10px;display:none;} 12 li{list-style:none;} 13 .list ul li a{height:30px;float:left;width:80px;text-decoration: none;color:gray;} 14 </style> 15 <script> 16 window.onload = function(){ 17 var oSelect = document.getElementsByTagName('select')[0]; 18 var oDiv = document.getElementById('list'); 19 oSelect.onclick = function(){ 20 if(this.value=="卡牌游戏"){ 21 oDiv.style.display="block"; 22 }else{ 23 oDiv.style.display="none"; 24 } 25 }; 26 }; 27 </script> 28 </head> 29 30 <body> 31 <div class="tab"> 32 <div class="menu"> 33 <span>游戏类型:</span> 34 <select> 35 <option>手机游戏</option> 36 <option>梦幻西游</option> 37 <option>卡牌游戏</option> 38 </select> 39 </div> 40 <div class="list" id="list"> 41 <ul> 42 <li><a href="javascript:;">动作游戏</a></li> 43 <li><a href="javascript:;">动作游戏</a></li> 44 <li><a href="javascript:;">动作游戏</a></li> 45 <li><a href="javascript:;">动作游戏</a></li> 46 <li><a href="javascript:;">动作游戏</a></li> 47 <li><a href="javascript:;">动作游戏</a></li> 48 <li><a href="javascript:;">动作游戏</a></li> 49 <li><a href="javascript:;">动作游戏</a></li> 50 <li><a href="javascript:;">动作游戏</a></li> 51 <li><a href="javascript:;">动作游戏</a></li> 52 53 </ul> 54 </div> 55 </div> 56 </body> 57 </html>