美文网首页
JS案例14-选择框

JS案例14-选择框

作者: hi__world | 来源:发表于2018-10-23 20:57 被阅读0次

简单版
缺点:单个移过去,顺序会变。

效果演示:


这个具体叫啥我也不知道

源码:


<!DOCTYPE html>
<html>
<head lang="en">
   <meta charset="UTF-8">
   <title>你不努力,怎么拿高薪,怎么养活你生命中最重要的两个女人</title>
   <style>
       select {
           width: 170px;
           height: 200px;
           font-size: 16px;
           background-color: #a4ff43;
       }
   </style>
</head>
<body>
   <select name="" id="sel1" size="10" multiple>
       <option value="0">香蕉</option>
       <option value="1">苹果</option>
       <option value="2" selected="">鸭梨</option>
       <option value="3">葡萄</option>
   </select>
   <input type="button" value=">>>"/>
   <input type="button" value="<<<"/>
   <input type="button" value=">"/>
   <input type="button" value="<"/>
   <select name="" id="sel2" size="10" multiple>

   </select>

   <script>
      var sel1=document.querySelector("#sel1");
      var sel2=document.querySelector("#sel2");
      var inp=document.querySelectorAll("input");

      inp[0].onclick=function(){
           for(var i=sel1.children.length-1;i>0;i--){
               sel2.appendChild(sel1.children[i])
           }
      }

      inp[1].onclick=function(){
           for(var i=sel2.children.length-1;i>=0;i--){
               sel1.appendChild(sel2.children[0])
           }
      }

       inp[2].onclick=function(){
           for(var i=sel1.length-1;i>=0;i--){
               if(sel1.children[i].selected===true){
                   sel2.appendChild(sel1.children[i])
               }
           }
       }

       inp[3].onclick=function(){
           for(var i=sel2.length-1;i>=0;i--){
               if(sel2.children[i].selected===true){
                   sel1.appendChild(sel2.children[i])
               }
           }
       }

   </script>


</body>
</html>

完美版

源码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        select {
            width: 150px;
            height: 200px;
            background-color: #7bff68;
        }
    </style>
</head>
<body>
<select size="10" name="aaa" id="sel1" multiple="multiple">
    <option value="0">1香蕉</option>
    <option value="1">2苹果</option>
    <option value="2">3大鸭梨</option>
    <option value="3">4草莓</option>
</select>

<input type="button" value=">>>"/>
<input type="button" value="<<<"/>
<input type="button" value=">"/>
<input type="button" value="<"/>

<select size="10" name="bbb" id="sel2" multiple="multiple">

</select>

<script>
    //需求:点击按钮把对应的选中项移动到另一侧。
    //技术点:如果移动单一的选项,那么看看哪个选项是有selected的。
            //如果移动所有的选项,那么直接把sel1中的所有选项放入sel2中。

    //步骤:
    //1.获取事件源和相关元素
    //2.绑定事件
    //3.书写事件驱动程序

    //步骤:
    //1.获取事件源和相关元素
    var sel1 = document.getElementById("sel1");
    var sel2 = document.getElementById("sel2");
    var inpArr = document.getElementsByTagName("input");

    //2.绑定事件(push和appendChild用法相似:但是一个是控制数组,一个是控制元素节点)
    inpArr[0].onclick = function () {
        var optArr = sel1.children;
        for(var i=0;i<optArr.length;){
            sel2.appendChild(optArr[i]);
        }
    }

    //为第二个按钮绑定事件
    inpArr[1].onclick = function () {
        var optArr = sel2.children;
        for(var i=0;i<optArr.length;){
            sel1.appendChild(optArr[i]);
        }
    }
    inpArr[2].onclick = function () {
        var optArr = sel1.children;
        for(var i=optArr.length-1;i>=0;i--){
            if(optArr[i].selected==true){
                optArr[i].selected=false;
                sel2.appendChild(optArr[i]);
            }
        }
        //获取sel2中的子元素变成真数组,然后排序
        var aaa = Array.from(sel2.children).sort(function (a,b) {
            return a.value-b.value;
        });
        //删除所有有子元素
        for(var i=0;i<sel2.children.length;i++){
            sel2.removeChild(sel2.children[i]);
        }
        //把排好序的数组添加到sel2中
        for(var i=0;i<aaa.length;i++){
            sel2.appendChild(aaa[i]);
        }
    }
    inpArr[3].onclick = function () {
        var optArr = sel2.children;
        for(var i=optArr.length-1;i>=0;i--){
            if(optArr[i].selected==true){
                optArr[i].selected=false;
                sel1.appendChild(optArr[i]);
            }
        }
        var aaa = Array.from(sel1.children).sort(function (a,b) {
            return a.value-b.value;
        });
        for(var i=0;i<sel1.children.length;i++){
            sel1.removeChild(sel1.children[i]);
        }
        for(var i=0;i<aaa.length;i++){
            sel1.appendChild(aaa[i]);
        }
    }
</script>

</body>
</html>

相关文章

网友评论

      本文标题:JS案例14-选择框

      本文链接:https://www.haomeiwen.com/subject/cvhnzftx.html