代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Author" CONTENT="majianan">
<script language="javascript" >
var currentSel = null;
function move(){
if(arguments.length==1){
moveUp(arguments[0]);
}else if(arguments.length==2){
moveRight(arguments[0],arguments[1]);
}
}
function moveUp(direction){
if(currentSel == null) return;
var index = currentSel.selectedIndex;
if(direction){//up
if(index==0) return;

var value = currentSel.options[index-1].value;
var text = currentSel.options[index-1].text;

currentSel.options[index-1].value = currentSel.options[index].value;
currentSel.options[index-1].text = currentSel.options[index].text;

currentSel.options[index].value = value;
currentSel.options[index].text = text;

currentSel.options[index].selected = false;
currentSel.options[index-1].selected = true;
}else{//down
if(index==(currentSel.length-1)) return;

var value = currentSel.options[index+1].value;
var text = currentSel.options[index+1].text;

currentSel.options[index+1].value = currentSel.options[index].value;
currentSel.options[index+1].text = currentSel.options[index].text;

currentSel.options[index].value = value;
currentSel.options[index].text = text;

currentSel.options[index].selected = false;
currentSel.options[index+1].selected = true;
}
}
function moveRight(src,des){
if(src.selectedIndex==-1){
alert("Please select first!");
return;
}
for(var i=0;i<src.length;i++){
if(src[i].selected){
var op = document.createElement("option");
op.value = src.options[src.selectedIndex].value;
op.text = src.options[src.selectedIndex].text;
des.options.add(op);
src.remove(i);
i--;
}
}
}
function setButton(obj){
if(obj.length==0) return;
currentSel = obj;
if(obj.id=="leftSel"){
document.getElementById("btnLeft").disabled = true;
document.getElementById("btnRight").disabled = false;

reSelect(document.getElementById("rightSel"));
}else{
document.getElementById("btnLeft").disabled = false;
document.getElementById("btnRight").disabled = true;

reSelect(document.getElementById("leftSel"));
}
}

function reSelect(obj){
for(var i=0; i<obj.length; i++){
if(obj[i].selected) obj[i].selected = false;
}
}
</script>
</HEAD>

<BODY>
<form id="form1">
<table width="40%" align="center">
<tr>
<td>
<input type="button" value=" Up " id="btnUp" onClick="move(true);" style="width:65"/>

<input type="button" value=" Down " id="btnDowm" onClick="move(false);" style="width:65" />
</td>
<td>
<select multiple id="leftSel" οnclick="setButton(this)" style="height:200px;width:100px;">
<option value="1">Java</option>
<option value="2">JavaScript</option>
<option value="3">C++</option>
<option value="4">HTML</option>
</select>
</td>
<td>
<input type="button" value=" >> " id="btnRight" onClick="move(document.getElementById('leftSel'),document.getElementById('rightSel'));" style="width:65" />

<input type="button" value=" << " id="btnLeft" onClick="move(document.getElementById('rightSel'),document.getElementById('leftSel'));" style="width:65" />
</td>
<td>
<select multiple id="rightSel" οnclick="setButton(this)" style="height:200px;width:100px;" >
<option value="5">CSS</option>
<option value="6">.Net</option>
</select>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>

效果图

转载于:https://www.cnblogs.com/diviner926934/p/6957907.html

JavaScript Select和Option列表元素上下左右移动相关推荐

  1. javascript select option对象总结

    javascript select option对象总结 一基础理解: var e = document.getElementById("selectId"); e. option ...

  2. 怎样得到select所有option里的值

    问: 一个页面里有个复选select 因为option是动态变化的 我想在提交后得到这个select里所有option的value <select multiple name=test> ...

  3. html 中的下拉列表,【HTML】什么是 HTML 中的下拉列表(select、option)

    我们以英雄联盟大区服务器状态查询网站里的"下拉选择服务器状态"为例,先来看一下,什么是下拉列表,浏览器运行效果如下所示这就是下拉列表用到的地方之一,接下来我们就来学习一下HTML中 ...

  4. Javascript - Select操作大集合

    1.向Select里添加Option function fnAddItem(text,value)         {             var selTarget = document.get ...

  5. js 操作select和option

    2019独角兽企业重金招聘Python工程师标准>>> 1.动态创建selectfunction createSelect(){var mySelect = document.cre ...

  6. CSS-5 列表元素(ol+ul++li+dl+dt+dd)、表格元素、单元格合并、表单元素(input+label+radio+...)、Emmet语法、结构伪类(:nth-child)

    目录 1_列表元素 1.1_列表的实现方式 1.2_有序列表 – ol – li 1.3_无序列表 – ul - li 1.4_定义列表 – dl – dt - dd 1.5_ 写前端代码逻辑顺序 2 ...

  7. html中select标记表示,如何在HTML中实现Select和Option标记

    选择和操作是HTML中最重要的标记之一,不能创建下拉菜单而不使用它.让我们以下面的方式开始我们在HTML中选择和选项标记的旅程:什么是HTML中的选择和选项标记?HTML中的Se 选择和操作是HTML ...

  8. 如何在JavaScript中实现链接列表

    If you are learning data structures, a linked list is one data structure you should know. If you do ...

  9. 轻量函数式 JavaScript:八、列表操作

    你在前一章闭包/对象的兔子洞中玩儿的开心吗?欢迎回来! 如果你能做很赞的事情,那就反复做. 我们在本书先前的部分已经看到了对一些工具的简要引用,现在我们要非常仔细地看看它们,它们是 map(..).f ...

最新文章

  1. python中的logging记录日志_[ Python入门教程 ] Python中日志记录模块logging使用实例...
  2. 【机器学习基础】在机器学习项目中该如何选择优化器
  3. php 数据显示,数据显示处理,该怎么处理
  4. [C/C++]C++标准
  5. BlueTooth 蓝牙音频音质探讨
  6. IT互联网公司的笔试的输入输出- c++ python
  7. Qt OpenGL 图像的平移(使用glTranslatef)
  8. 矢量图和Word:EPS,PDF,EMF和SVG
  9. Java高级开发面试,Java面试没有项目经验
  10. 【Shell】检查字符串大小
  11. 安卓深度探索(卷一)第六章
  12. 【Python】实现商品信息管理系统(界面版,附带数据库)
  13. keil编译器CODE、RO、RW、ZI的含义
  14. linux下怎么玩模拟人生4,《模拟人生4》典藏版晶锥灯在非游戏状态下的使用图文教程...
  15. 【C语言】形参实参以及参数传递
  16. 安装Win10 Ubuntu20.04双系统
  17. 数据中心的功率密度将会得到进一步提高
  18. Snipaste-无广告截图工具
  19. 分治法查找数组元素的最大值和最小值(python实现)
  20. 计算机应用与需求相结合,计算机应用人才培养与企业需求的有效对接

热门文章

  1. CTFshow——Pwn(1)
  2. Android虚拟机-Dalvik VM架构
  3. 广西2021高考成绩什么时候可以查询,2021年广西高考成绩什么时候公布出来,几月几号几点钟可以查询...
  4. 扩容是元素还是数组_348,数据结构1,数组
  5. 电影中的出品人,制片人
  6. hibernate异常:not-null property references a null or transient value
  7. 黑客游戏系列--------第四关
  8. SpringBoot的yml配置文件
  9. oracle数据库常用关键字以及写法
  10. 第 3 章 第 2 题 求级数问题 递归法实现