重要属性介绍:
ondblclick=selectone():双击事件
select标签的属性:
multiple=multiple:
看一下实现效果:
具体实现代码:
<!doctype html><html> <head> <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 style="float: left;"> 已有商品<br /> <select multiple="multiple" id="leftselect" ondblclick="selectone()"> <option>华为</option> <option>小米</option> <option>锤子</option> <option>oppo</option> </select> <br /> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="selectone()"> >> </a> <br /> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="selectall()"> >>> </a> </div> <!--右边--> <div style="float: right;"> 未有商品<br /> <select multiple="multiple" id="rightselect"> <option>苹果6</option> <option>肾7</option> <option>诺基亚</option> <option>波导</option> </select> <br /> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > << </a> <br /> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <<< </a> </div> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> </td> </tr> </table> </body></html>
推荐教程:js入门教程
以上就是js如何实现下拉控制列表的详细内容。