10
0

练习13_穿梭框.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box1,.box2{
  9. float: left;
  10. margin-right: 20px;
  11. }
  12. select{
  13. width: 100px;
  14. height: 200px;
  15. }
  16. button{
  17. display: block;
  18. margin-top: 20px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box1">
  24. <select multiple id="sel-left">
  25. <option>选项一</option>
  26. <option>选项二</option>
  27. <option>选项三</option>
  28. <option>选项四</option>
  29. <option>选项五</option>
  30. <option>选项六</option>
  31. </select>
  32. <button id="sel-left-btn">选中移动到右侧</button>
  33. <button id="all-left-btn">全部移动到右侧</button>
  34. </div>
  35. <div class="box2">
  36. <select multiple id="sel-right">
  37. <option>选项七</option>
  38. </select>
  39. <button id="sel-right-btn">选中移动到左侧</button>
  40. <button id="all-right-btn">全部移动到左侧</button>
  41. </div>
  42. <script>
  43. var oSelLeft = document.getElementById("sel-left");
  44. var oSelRight = document.getElementById("sel-right");
  45. var selLeftBtn = document.getElementById("sel-left-btn");
  46. var selRightBtn = document.getElementById("sel-right-btn");
  47. var allLeftBtn = document.getElementById("all-left-btn");
  48. var allRightBtn = document.getElementById("all-right-btn");
  49. // 左侧下拉列表的双击事件
  50. oSelLeft.ondblclick = function(){
  51. // console.log(this.selectedOptions);
  52. // console.log(this.options);
  53. // console.log(this.selectedIndex);
  54. // console.log(this.options[this.selectedIndex]);
  55. // 获取双击的选中项
  56. var selOption = this.selectedOptions[0];
  57. // 将选中项移动到右侧下拉列表中
  58. oSelRight.append(selOption);
  59. }
  60. // 右侧下拉列表的双击事件
  61. oSelRight.ondblclick = function(){
  62. var selOption = this.selectedOptions[0];
  63. oSelLeft.append(selOption);
  64. }
  65. // 左侧下拉框选中移动到右侧下拉框
  66. selLeftBtn.onclick = function(){
  67. var selOptions = oSelLeft.selectedOptions;
  68. var selArr = [];
  69. for(var j=0;j<selOptions.length;j++){
  70. selArr.push(selOptions[j]);
  71. }
  72. for(var i=0;i<selArr.length;i++){
  73. oSelRight.append(selArr[i])
  74. }
  75. }
  76. //右侧下拉框选中移动到左侧
  77. selRightBtn.onclick = function(){
  78. var selOptions = oSelRight.selectedOptions;
  79. var selArr = [];
  80. for(var i=0;i<selOptions.length;i++){
  81. selArr.push(selOptions[i]);
  82. }
  83. for(var j=0;j<selArr.length;j++){
  84. oSelLeft.append(selArr[j]);
  85. }
  86. }
  87. //左侧全部移动到右侧按钮
  88. allLeftBtn.onclick = function(){
  89. //方法一
  90. // var allOption = [];
  91. // for(var j=0;j<oSelLeft.options.length;j++){
  92. // allOption.push(oSelLeft.options[j])
  93. // }
  94. // for(var i=0;i<allOption.length;i++){
  95. // oSelRight.append(allOption[i]);
  96. // }
  97. // oSelRight.innerHTML = oSelRight.innerHTML + oSelLeft.innerHTML;
  98. oSelRight.innerHTML += oSelLeft.innerHTML;
  99. oSelLeft.innerHTML = "";
  100. }
  101. // 右侧全部移动到左侧按钮
  102. allRightBtn.onclick = function(){
  103. oSelLeft.innerHTML += oSelRight.innerHTML;
  104. oSelRight.innerHTML = "";
  105. }
  106. </script>
  107. </body>
  108. </html>