【JavaScript】列表元素上下左右移动:Select和Option的应用
马嘉楠 2008-12-26
共同学习,欢迎转载。转载请注明地址【http://blog.csdn.net/majianan/archive/2008/12/26/3614255.aspx】,谢谢O(∩_∩)O!
功能如下:
支持一次选中多项在左右列表中来回移动
<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
|
|
代码如下:
<!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 =
<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 =