国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

基于javascript實(shí)現(xiàn)listbox左右移動(dòng)

2019-11-20 10:39:42
字體:
供稿:網(wǎng)友

本文實(shí)例講解了javascript實(shí)現(xiàn)listbox左右移動(dòng)的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下

效果圖:

具體代碼:

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>listbox左右移動(dòng)</title> </head>  <body>  <div style="background-color:#CCC; width:450px; height:300px; margin:150px,0,0,450px; border:1px solid">   <table align="center" width="285" height="169" bgcolor="#99CCFF">   <tr>    <td width="100">     <select name="first" id="first" size="10" multiple="multiple" style="background-color:#3FC;">      <option value="選項(xiàng)1">選項(xiàng)1</option>      <option value="選項(xiàng)2">選項(xiàng)2</option>      <option value="選項(xiàng)3">選項(xiàng)3</option>      <option value="選項(xiàng)4">選項(xiàng)4</option>      <option value="選項(xiàng)5">選項(xiàng)5</option>      <option value="選項(xiàng)6">選項(xiàng)6</option>      <option value="選項(xiàng)7">選項(xiàng)7</option>      <option value="選項(xiàng)8">選項(xiàng)8</option>     </select>    </td>    <td width="85" valign="middle">     <input name="add" id="add" type="button" value="--->"/>     <input name="add_all" id="add_all" type="button" value="===>"/>     <input name="remove" id="remove" type="button" value="<---"/>     <input name="remove_all" id="remove_all" type="button" value="<==="/>    </td>    <td width="100" align="left">     <select name="second" id="second" size="10" multiple="multiple" style="background-color:#3FC;">     <option value="選項(xiàng)9">選項(xiàng)9</option>     </select>    </td>   </tr>   </table> </div>  </body> <script type="text/javascript">  //左移右     /*<input name="add" id="add" type="button" value="--->"/>*/   document.getElementById("add").onclick = function add()   {    var firstSel = document.getElementById("first");    var option = firstSel.getElementsByTagName("option");    //javascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。    //所以先取得下拉列表的長(zhǎng)度,避免option被移走后長(zhǎng)度變小,導(dǎo)致后面循環(huán)終止,出現(xiàn)beg    var oplength=option.length;    var secondSel = document.getElementById("second");    for(i=0;i<oplength;i++)    {      /*       selectedIndex: 該下標(biāo)返回下拉列表的索引值       注: 如果有多個(gè)被選中的情況下,永遠(yuǎn)返回第一個(gè)選中的索引值,索引最小的那個(gè)          如果沒有被選中的情況下,返回-1          selectedIndex是<select>的屬性      */      if(firstSel.selectedIndex!=-1)      {        secondSel.appendChild(option[firstSel.selectedIndex]);      }    }       }      /*<input name="add_all" id="add_all" type="button" value="===>"/>*/   document.getElementById("add_all").onclick = function addAll()   {    var firstSel = document.getElementById("first");    var option = firstSel.getElementsByTagName("option");    //javascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。    //所以先取得下拉列表的長(zhǎng)度,避免option被移走后長(zhǎng)度變小,導(dǎo)致后面循環(huán)終止,出現(xiàn)beg    var oplength=option.length;    var secondSel = document.getElementById("second");    for(i=0;i<oplength;i++)    {     /*因?yàn)閖avascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。所以當(dāng)移走全部把數(shù)     組的值移走(一個(gè)一個(gè)的移走,數(shù)組長(zhǎng)度馬上-1,所以數(shù)組下標(biāo)也是-1.因次我們要把每次移的是走下標(biāo)為0的那個(gè)     數(shù),這樣才保證可以全部移走)*/     secondSel.appendChild(option[0]);    }   }      /*雙擊后把option移到右邊*/   document.getElementById("first").ondblclick = function dblclick()   {     /*方法一*/     /*    var firstSel = document.getElementById("first");    var option = firstSel.getElementsByTagName("option");    //javascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。    //所以先取得下拉列表的長(zhǎng)度,避免option被移走后長(zhǎng)度變小,導(dǎo)致后面循環(huán)終止,出現(xiàn)beg    var oplength=option.length;    var secondSel = document.getElementById("second");    for(i=0;i<oplength;i++)    {       //雙擊可以看成:第一次點(diǎn)擊選中,第二次點(diǎn)擊移動(dòng)       secondSel.appendChild(option[firstSel.selectedIndex]);      }    */        /*方法二*/    /*    this: this表示document.getElementById("first")  下拉列表       this.selectedIndex表示下拉列表選中的項(xiàng)    */     var secondSel = document.getElementById("second");     secondSel.appendChild(this[this.selectedIndex]);   }               //右移左         /*<input name="remove" id="remove" type="button" value="<---"/>*/   document.getElementById("remove").onclick = function remove()   {    var secondSel = document.getElementById("second");    var firstSel = document.getElementById("first");    var option = secondSel.getElementsByTagName("option");    //javascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。    //所以先取得下拉列表的長(zhǎng)度,避免option被移走后長(zhǎng)度變小,導(dǎo)致后面循環(huán)終止,出現(xiàn)beg    var oplength=option.length;    for(i=0;i<oplength;i++)    {      /*       selectedIndex: 該下標(biāo)返回下拉列表的索引值       注: 如果有多個(gè)被選中的情況下,永遠(yuǎn)返回第一個(gè)選中的索引值,索引最小的那個(gè)          如果沒有被選中的情況下,返回-1          selectedIndex是<select>的屬性      */      if(secondSel.selectedIndex!=-1)      {        firstSel.appendChild(option[secondSel.selectedIndex]);      }    }       }      /*<input name="remove_all" id="remove_all" type="button" value="<==="/>*/   document.getElementById("remove_all").onclick = function remove_all()   {    var secondSel = document.getElementById("second");    var firstSel = document.getElementById("first");    var option = secondSel.getElementsByTagName("option");    //javascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。    //所以先取得下拉列表的長(zhǎng)度,避免option被移走后長(zhǎng)度變小,導(dǎo)致后面循環(huán)終止,出現(xiàn)beg    var oplength=option.length;    for(i=0;i<oplength;i++)    {     /*因?yàn)閖avascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。所以當(dāng)移走全部把數(shù)     組的值移走(一個(gè)一個(gè)的移走,數(shù)組長(zhǎng)度馬上-1,所以數(shù)組下標(biāo)也是-1.因次我們要把每次移的是走下標(biāo)為0的那個(gè)     數(shù),這樣才保證可以全部移走)*/     firstSel.appendChild(option[0]);    }   }      /*雙擊后把option移到右邊*/   document.getElementById("second").ondblclick = function dblclick()   {     /*方法一*/     /*    var secondSel = document.getElementById("second");    var firstSel = document.getElementById("first");    var option = secondSel.getElementsByTagName("option");    //javascript的數(shù)組是動(dòng)態(tài)數(shù)組,長(zhǎng)度是可以變的。    //所以先取得下拉列表的長(zhǎng)度,避免option被移走后長(zhǎng)度變小,導(dǎo)致后面循環(huán)終止,出現(xiàn)beg    var oplength=option.length;    for(i=0;i<oplength;i++)    {       //雙擊可以看成:第一次點(diǎn)擊選中,第二次點(diǎn)擊移動(dòng)       firstSel.appendChild(option[secondSel.selectedIndex]);      }    */        /*方法二*/    /*    this: this表示document.getElementById("second")  下拉列表       this.selectedIndex表示下拉列表選中的項(xiàng)    */     var firstSel = document.getElementById("first");     firstSel.appendChild(this[this.selectedIndex]);   } </script> </html> 

代碼注釋很詳細(xì),希望可以幫到大家。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沧源| 泽库县| 思南县| 体育| 汝州市| 花垣县| 南昌市| 青冈县| 西昌市| 芮城县| 东阳市| 丹棱县| 张掖市| 天津市| 当涂县| 凤阳县| 宁波市| 保德县| 南宁市| 临安市| 靖远县| 阿拉善盟| 秭归县| 克山县| 陕西省| 滦平县| 巩义市| 永仁县| 舞阳县| 集贤县| 商城县| 赣榆县| 慈溪市| 平果县| 嘉禾县| 彩票| 大冶市| 乐昌市| 抚松县| 浠水县| 竹山县|