廢話不多說(shuō),先給大家展示一下效果圖,如果大家感覺不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼。
效果圖如下所示:
代碼如下所示:
<!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> <script language="javascript" type="text/javascript"> function moveOption(e1, e2){ try{ for(var i=0;i<e1.options.length;i++){ if(e1.options[i].selected){ var e = e1.options[i]; e2.options.add(new Option(e.text, e.value)); e1.remove(i); ii=i-1; } } document.form1.city.value=getvalue(document.form1.list2); }catch(e){ } } function getvalue(geto){ var allvalue = ""; for(var i=0;i<geto.options.length;i++){ allvalue +=geto.options[i].value + ","; } return allvalue; } function changepos(obj,index) { if(index==-1){ if (obj.selectedIndex>0){ //obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex-1)) //swapNode方法只有IE才支持 obj.insertBefore(obj.options[obj.selectedIndex], obj.options[obj.selectedIndex - 1]); } }else if(index==1){ if (obj.selectedIndex<obj.options.length-1){ //obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex+1)) //swapNode方法只有IE才支持 obj.insertBefore(obj.options[obj.selectedIndex + 1], obj.options[obj.selectedIndex]); } } } </script> <style type="text/css"> body { font-size: 16px; color: #003300; } </style> </head> <body> <form method="post" name="form1" action="" style="text-align:center;"> <table border="0" width="300" align="center"> <tr> <td width="40%"><select style="width:100%" multiple name="list1" size="12" ondblclick="moveOption(document.form1.list1, document.form1.list2)"> <option value="張三">張三</option> <option value="李四">李四</option> <option value="王五">王五</option> <option value="趙六">趙六</option> <option value="錢七">錢七</option> <option value="軟件">軟件</option> <option value="客服">客服</option> <option value="硬件">硬件</option> <option value="安全">安全</option> <option value="會(huì)計(jì)">會(huì)計(jì)</option> <option value="出納">出納</option> </select></td> <td width="20%" align="center"><input type="button" value="添加" onclick="moveOption(document.form1.list1, document.form1.list2)"/> <br/> <br/> <input type="button" value="刪除" onclick="moveOption(document.form1.list2, document.form1.list1)"/></td> <td width="40%"><select style="width:100%" multiple name="list2" size="12" ondblclick="moveOption(document.form1.list2, document.form1.list1)"> </select></td> <td><input type="button" value="上移" onclick="changepos(list2,-1)"/> <br/> <br/> <input type="button" value="下移" onclick="changepos(list2,1)"/></td> </tr> </table> 值: <input type="text" name="city" size="40" /> </form> <p align="center">選定一項(xiàng)或多項(xiàng)然后點(diǎn)擊添加或移除(按住shift或ctrl可以多選),<br /> 或在選擇項(xiàng)上雙擊進(jìn)行添加和移除。</p> </body> </html>
以上所述是小編給大家介紹的基于JavaScript實(shí)現(xiàn)下拉列表左右移動(dòng),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注