html如何实现左右菜单左右互换
更新:HHH   时间:2023-1-7


这篇文章将为大家详细讲解有关html如何实现左右菜单左右互换,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

<meta charset="UTF-8">
        <title></title>
        <!--
            步骤分析
                1. 确定事件: 点击事件 :onclick事件
                2. 事件要触发函数 selectOne
                3. selectOne要做一些操作
                    (将左边选中的元素移动到右边的select中)
                    1. 获取左边Select中被选中的元素
                    2. 将选中的元素添加到右边的Select中就可以
        -->
        <script>

            function selectOne(){
//              1. 获取左边Select中被选中的元素
                var leftSelect = document.getElementById("leftSelect");
                var options = leftSelect.options;

                //找到右侧的Select
                var rightSelect = document.getElementById("rightSelect");
                //遍历找出被选中的option
                for(var i=0; i < options.length; i++){
                    var option1 = options[i];
                    if(option1.selected){
        //              2. 将选中的元素添加到右边的Select中就可以
                        rightSelect.appendChild(option1);
                    }
                }
            }

            //将左边所有的商品移动到右边
            function selectAll(){
//              1. 获取左边Select中被选中的元素
                var leftSelect = document.getElementById("leftSelect");
                var options = leftSelect.options;

                //找到右侧的Select
                var rightSelect = document.getElementById("rightSelect");
                //遍历找出被选中的option
                for(var i=options.length - 1; i >=0; i--){
                    var option1 = options[i];
                    rightSelect.appendChild(option1);
                }
            }

        </script>
    </head>
    <body>

        <table border="1px" width="400px">
            <tr>
                <td>分类名称</td>
                <td><input type="text" value="手机数码"/></td>
            </tr>
            <tr>
                <td>分类描述</td>
                <td><input type="text" value="这里面都是手机数码"/></td>
            </tr>
            <tr>
                <td>分类商品</td>
                <td>
                    <!--左边-->
                    <div >
                        已有商品<br />
                        <select multiple="multiple" id="leftSelect" ondblclick="selectOne()">
                            <option>华为</option>
                            <option>小米</option>
                            <option>锤子</option>
                            <option>oppo</option>
                        </select>
                        <br />
                        <a href="#" onclick="selectOne()"> &gt;&gt; </a> <br />
                        <a href="#" onclick="selectAll()"> &gt;&gt;&gt; </a>
                    </div>
                    <!--右边-->
                    <div > 
                        未有商品<br />
                        <select multiple="multiple" id="rightSelect">
                            <option>苹果6</option>
                            <option>泰康手机</option>
                            <option>诺基亚</option>
                            <option>三星</option>
                        </select>
                        <br />
                        <a href="#"> &lt;&lt; </a> <br />
                        <a href="#"> &lt;&lt;&lt; </a>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交"/>
                </td>
            </tr>
        </table>

    </body>

关于“html如何实现左右菜单左右互换”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

返回web开发教程...