Oo緣來是你oO


          posts - 120,comments - 125,trackbacks - 0
           

           【JavaScript】列表(Select)選項(Option)的移動(上下左右)

          馬嘉楠    2009-1-11

          共同學習,歡迎轉載。轉載請注明地址【http://blog.csdn.net/majianan/archive/2009/01/11/3754521.aspx】,謝謝O(∩_∩)O!

          對《【JavaScript】列表元素上下左右移動:Select和Option的應用》中的方法進行了優化。

          1.使用appendChild()方法優化左右移動函數moveRight()

          2.使用insertBefore()方法優化上下移動函數moveUp()

          3.修改同時選中多項上下移動不正常的Bug

           

          功能如下:

          支持一次選中多項在左右列表中來回移動

           



           

           

           

          代碼如下:

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
          <HTML>
           
          <HEAD>
            
          <TITLE> New Document </TITLE>
            
          <META NAME="Author" CONTENT="majianan">
          <SCRIPT language=javascript>
              
          var currentSel = null;
              
          function
           setButton(obj){        
                  
          if(obj.length==0return
          ;
                  currentSel 
          =
           obj;
                  
          if(obj.id=="leftSel"
          ){
                      document.getElementById(
          "btnLeft").disabled = true
          ;
                      document.getElementById(
          "btnRight").disabled = false
          ;                
                      reSelect(document.getElementById(
          "rightSel"
          ));            
                  }
          else
          {
                      document.getElementById(
          "btnLeft").disabled = false
          ;
                      document.getElementById(
          "btnRight").disabled = true
          ;               
                      reSelect(document.getElementById(
          "leftSel"
          ));                
                  }       
              }

                
          function
           move(){
                    
          if(arguments.length==1
          ){
                        moveUp(arguments[
          0
          ]);
                    }
          else if(arguments.length==2
          ){
                        moveRight(arguments[
          0],arguments[1
          ]);
                    }
                }

                
          function
           moveUp(direction){
                    
          if(currentSel == nullreturn
          ;
                    
          if(direction){//up

                      if (currentSel.selectedIndex > 0) {  
                          
          for(var i=0;i<currentSel.length;i++
          ){
                              
          if
          (currentSel[i].selected){
                                  
          var oOption =
           currentSel.options[i];
                                  
          var oPrevOption = currentSel.options[i---1
          ];
                                  currentSel.insertBefore(oOption, oPrevOption);
                              }
                          }                
                      } 
                    }
          else{//down

                      for(var i=currentSel.length-1;i>=0;i--){
                          
          if
          (currentSel[i].selected){
                              
          if(i==currentSel.length-1return
          ;
                              
          var oOption =
           currentSel.options[i];
                              
          var oNextOption = currentSel.options[i+1
          ];
                              currentSel.insertBefore(oNextOption, oOption);                        
                          }
                      }
                    }
                }

              
          function
           moveRight(src,des){
                  
          if(src.selectedIndex==-1
          ){
                      alert(
          "Please select first!"
          );
                      
          return
          ;
                  }
                  
          for(var i=0;i<src.length;i++
          ){
                      
          if
          (src[i].selected){
                          des.appendChild(src.options[i
          --
          ]);
                      }
                  }
                  setButton(des);
              }
              
              
          function
           reSelect(obj){
                  
          for(var i=0; i<obj.length; i++
          ){
                      
          if(obj[i].selected) obj[i].selected = false
          ;
                  }
              }
              
          </SCRIPT>


           
          </HEAD>

           
          <BODY>
            
          <form id="form1">
                
          <table width="40%" align="center">
                    
          <tr>
                        
          <td>
                            
          <input type="button" value=" Up " id="btnUp" onClick="move(true);" style="width:65"/>
                          
          <br />
                          
          <input type="button" value=" Down " id="btnDowm" onClick="move(false);" style="width:65" />                
                        
          </td>

                        
          <td>
                            
          <select multiple id="leftSel" onclick="setButton(this)" ondblclick="document.getElementById('btnRight').click()" style="height:200px;width:100px;">

                              
          <option value="1">Java</option>
                              
          <option value="2">JavaScript</option>
                              
          <option value="3">C++</option>
                              
          <option value="4">HTML</option>
                          
          </select>
                      
          </td>
                      
          <td>
                          
          <input type="button" value=" >> " id="btnRight" onClick="move(document.getElementById('leftSel'),document.getElementById('rightSel'));" style="width:65" />
                          
          <br />
                          
          <input type="button" value=" << " id="btnLeft" onClick="move(document.getElementById('rightSel'),document.getElementById('leftSel'));" style="width:65" />
                          
          </td>
                          
          <td>
                              
          <select multiple id="rightSel" onclick="setButton(this)"  ondblclick="document.getElementById('btnLeft').click()" style="height:200px;width:100px;" >
           
                              
          <option value="5">CSS</option>
                              
          <option value="6">.Net</option>
                              
          </select>
                          
          </td>
                      
          </tr>
                  
          </table>
              
          </form>
           
          </BODY>
          </HTML>



          馬嘉楠
          jianan.ma@gmail.com

          posted on 2009-01-11 22:58 馬嘉楠 閱讀(799) 評論(0)  編輯  收藏 所屬分類: SoureCodeJava Script
          主站蜘蛛池模板: 兴文县| 晋宁县| 马公市| 运城市| 巩义市| 万盛区| 东兰县| 石棉县| 绵竹市| 合肥市| 南丰县| 宜兰县| 武功县| 比如县| 壤塘县| 嵊州市| 普兰县| 阿克| 巴东县| 台南县| 姜堰市| 柘城县| 额敏县| 黔南| 思南县| 望城县| 霍山县| 黄骅市| 平湖市| 揭东县| 江口县| 荥经县| 榆林市| 丁青县| 桑日县| 偃师市| 亳州市| 徐水县| 体育| 扶沟县| 营口市|