JavaScript實(shí)現(xiàn)下拉列表選擇框
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)下拉列表選擇框的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)建一個頁面
** 兩個下拉選擇框
- 設(shè)置屬性 multiple屬性 -multiple="multiple"(下拉選擇框多行顯示)
** 四個按鈕,有事件
tip:多選按住ctrl或者shift點(diǎn)擊選項(xiàng)
代碼如下:
<html >
<head>
<title>HTML示例</title>
<style type = "text/css">
div#left{
float:left;
}
</style>
</head>
<body>
<div id="left" ">
<div >
<select id="select1" multiple="multiple" style="width:100px;height:1ss00px">
<option>AAAAAA</option>
<option>BBBBBB</option>
<option>CCCCCC</option>
<option>DDDDDD</option>
<option>EEEEEE</option>
</select><br/>
</div>
<div>
<input type="button" value="選中添加到右邊" onclick="selToRight()"/><br/>
<input type="button" value="全部添加到右邊" onclick="selAllRight()"/>
</div>
</div>
<div id="right">
<div >
<select id="select2" multiple="multiple" style="width:100px;height:1ss00p">
<option>FFFFFF</option>
</select><br/>
</div>
<div>
<input type="button" value="選中添加到左邊" onclick="selToLeft()"/><br/>
<input type="button" value="全部添加到左邊" onclick="selAllLeft()"/>
</div>
</div>
</body>
<script type="text/javascript">
// 選中添加到左邊
function selToLeft(){
//獲取左邊select對象
var s1 = document.getElementById("select1");
//獲取右邊select對象
var s2 = document.getElementById("select2");
//得到左邊select對象中的每一個option
var ops = s2.getElementsByTagName("option");
for(var i4=0;i4<ops.length;i4++){
op4=ops[i4];
if(op4.selected==true){
s1.appendChild(op4);
i4--;
}
}
}
//全部添加到左邊
function selAllLeft(){
//獲取左邊select對象
var s1 = document.getElementById("select1");
//獲取右邊select對象
var s2 = document.getElementById("select2");
//得到左邊select對象中的每一個option
var ops = s2.getElementsByTagName("option");
for(var i3=0;i3<ops.length;i3++){
op3=ops[i3];
s1.appendChild(op3);
i3--;
}
}
//全部添加到右邊
function selAllRight(){
//獲取左邊select對象
var s1 = document.getElementById("select1");
//獲取右邊select對象
var s2 = document.getElementById("select2");
//得到左邊select對象中的每一個option
var ops = s1.getElementsByTagName("option");
for(var i2=0;i2<ops.length;i2++){
op2=ops[i2];
s2.appendChild(op2);
i2--;
}
}
//選中添加到右邊
function selToRight(){
/*
步驟:
1.獲取select里面的option
-getElementByTagName()-返回一個數(shù)組
-遍歷數(shù)組,得到每一個option
2.判斷option是否被選中
-屬性selected,判斷是否被選中
-selected=true;選中
-selected=false;未選中
3.如果選中,把選中的添加到右邊
4.得到select2
5.添加選擇的部分
-appendChild()方法
*/
//獲取左邊select對象
var s1 = document.getElementById("select1");
//獲取右邊select對象
var s2 = document.getElementById("select2");
//得到左邊select對象中的每一個option
var ops = s1.getElementsByTagName("option");
//遍歷ops數(shù)組得到每一個option選中狀態(tài)
for(var i1=0;i1<ops.length;i1++){
op1 = ops[i1];
//判斷每一個option中selected屬性是否選中
if(op1.selected == true){
//如果選中,添加到右邊select中
//-使用appendChild()方法
s2.appendChild(op1);
//每次添加都會使數(shù)組長度減一,i1++后長度出現(xiàn)異常,所以我們要--;
i1--;
}
}
}
</script>
</html>
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php register_shutdown_function函數(shù)詳解
register_shutdown_function() 函數(shù)可實(shí)現(xiàn)當(dāng)程序執(zhí)行完成后執(zhí)行的函數(shù),其功能為可實(shí)現(xiàn)程序執(zhí)行完成的后續(xù)操作,需要的朋友可以參考下2017-07-07
基于JavaScript實(shí)現(xiàn)網(wǎng)頁版羊了個羊游戲
最近羊了個羊火的不得了,這篇文章主要為大家介紹了如何利用JS實(shí)現(xiàn)個網(wǎng)頁版羊了個羊游戲,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-09-09
微信小程序?qū)崿F(xiàn)的picker多級聯(lián)動功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)的picker多級聯(lián)動功能,結(jié)合實(shí)例形式分析了微信小程序picker組件使用及wx.request后臺交互相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Axios?get?post請求傳遞參數(shù)的實(shí)現(xiàn)代碼
axios是基于promise用于瀏覽器和node.js的http客戶端,支持瀏覽器和node.js,能攔截請求和響應(yīng),這篇文章主要介紹了axios?get?post請求傳遞參數(shù)的操作代碼,需要的朋友可以參考下2022-11-11
ES5 模擬 ES6 的 Symbol 實(shí)現(xiàn)私有成員功能示例
這篇文章主要介紹了ES5 模擬 ES6 的 Symbol 實(shí)現(xiàn)私有成員功能,結(jié)合實(shí)例形式分析了ES5 模擬 ES6 的 Symbol 實(shí)現(xiàn)私有成員功能相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05

