<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
//flag标实为全部移动(true) 还是选择性移动(false)
function mymove(flag,lid,rid){
var leftobj = document.getElementById(lid);//得到左边select元素对象
var rightobj = document.getElementById(rid);//得到右边select元素对象
for( var i = 0 ; i < leftobj.options.length ; i++ ){
if(flag){
rightobj.appendChild(leftobj.options[i]);
i--;
}else{
if( leftobj.options[i].selected ){
rightobj.appendChild(leftobj.options[i]);
i--;
}
}
}
}
</script>
</head>
<body>
<input type="button" value="全部左移" onclick="mymove(true,'rightid','leftid')"/>
<input type="button" value="左移" onclick="mymove(false,'rightid','leftid')"/>
<input type="button" value="右移" onclick="mymove(false,'leftid','rightid')"/>
<input type="button" value="全部右移" onclick="mymove(true,'leftid','rightid')"/>
<hr>
<select size="6" multiple="multiple" style="width: 50px; height: 120px;" id="leftid">
<option>武汉</option>
<option>北京</option>
<option>上海</option>
<option>广州</option>
<option>杭州</option>
<option>黄石</option>
</select>
<select size="6" multiple="multiple" style="width: 50px; height: 120px; margin-left: 100px;" id="rightid">
</select>
</body>
</html>
js实现下拉菜单左右移动
最新推荐文章于 2021-06-03 01:47:03 发布