水果移动

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        select {
            width: 200px;
            height: 200px;
            background-color: #33cccc;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <select id="all" size="5" multiple="multiple">
        <!-- multiple可以实现多选效果 -->
        <!-- <option>苹果</option>
        <option>橘子</option>
        <option>梨</option>
        <option>西瓜</option>
        <option>水蜜桃</option> -->
    </select>

    <input type="button" value=">>" id="btn1">
    <input type="button" value="<<" id="btn2">
    <input type="button" value=">" id="btn3">
    <input type="button" value="<" id="btn4">

    <select id="choose" multiple="multiple">
    </select>

    <script>
        var all = document.getElementById("all");
        var choose = document.getElementById("choose");

        var btn1 = document.getElementById("btn1");
        var btn2 = document.getElementById("btn2");
        var btn3 = document.getElementById("btn3");
        var btn4 = document.getElementById("btn4");

        var options = ["苹果","橘子","西瓜","梨","水蜜桃"];

        for(var i=0;i<options.length;i++){
            var option = document.createElement("option");
            option.innerText = options[i];
            all.appendChild(option);
        }
        

        //btn1移动all中所有元素到choose中
        btn1.onclick = function(){
            var opts = all.children; //内部的元素是动态添加的
            //获取最初的数组长度
            var n = opts.length;
            for(var i=0;i<n;i++){
                choose.appendChild(opts[0]);
            }
        };

        //btn2
        btn2.onclick = function(){
            var opts = choose.children; //内部的元素是动态添加的
            //获取最初的数组长度
            var n = opts.length;
            for(var i=0;i<n;i++){
                all.appendChild(opts[0]);
            }
        };

        //btn3单选/多选(Ctrl)
        btn3.onclick = function(){
            var opts = all.children;

            //将需要移动的元素放到数组中
            var arr= [];
            for(var i =0;i<opts.length;i++){
                if(opts[i].selected == true){
                    arr.push(opts[i]);
                }
            }

            console.log(arr);

            for(var j=0;j<arr.length;j++){
                choose.appendChild(arr[j]);
                arr[j].selected = false;
            }

        };

        //btn4
        btn4.onclick = function(){
            var opts = choose.children;
            var arr=[];
            for(var i=0;i<opts.length;i++){
                if(opts[i].selected == true){
                    arr.push(opts[i]);
                }
            }

            console.log(arr);

            for(var j = 0;j<arr.length;j++){
                all.appendChild(arr[j]);
                arr[j].selected = false;
            }
        }
    </script>
</body>

</html>
posted @ 2021-08-14 19:30  charonmomo  阅读(25)  评论(0编辑  收藏  举报