javascript基于DOM实现权限选择实例分析
本文实例讲述了javascript基于DOM实现权限选择的方法。分享给大家供大家参考。具体实现方法如下:
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>权限选择</title>
<scripttype="text/javascript">
//====================多选操作====================================
functionselMultiple(selectSrc,selectDes){
for(vari=selectSrc.childNodes.length-1;i>=0;i--){
varoption=selectSrc.childNodes[i];
if(option.selected==true){
selectSrc.removeChild(option);
option.selected=false;
selectDes.appendChild(option);
}
}
}
functionselectToRight(){
varselectSrc=document.getElementById("select1");
varselectDes=document.getElementById("select2");
selMultiple(selectSrc,selectDes);
}
functionselectToLeft(){
varselectSrc=document.getElementById("select2");
varselectDes=document.getElementById("select1");
selMultiple(selectSrc,selectDes);
}
//====================全选操作====================================
functionselAll(selectSrc,selectDes){
//这种写法有问题,发现selectSrc.childNodes.length居然等于10,实际上只有5个元素
//for(vari=0;i<selectSrc.childNodes.length;i++){
//varoption=selectSrc.childNodes[0];
//selectSrc.removeChild(option);
//selectDes.appendChild(option);
//}
varoptions=selectSrc.getElementsByTagName("option");
varoptLength=options.length;
/*
注意:for循环中不能直接使用options.length,因为selectDes.appendChild执行后
会导致options.length减一,所以先把options.length存放到一个变量中备用
*/
for(vari=0;i<optLength;i++){
varoption=options[0];//这里使用的始终是第0个元素
selectDes.appendChild(option);
}
selectSrc.options.length=0;
}
functionselectToRightAll(){
varselectSrc=document.getElementById("select1");
varselectDes=document.getElementById("select2");
selAll(selectSrc,selectDes);
}
functionselectToLeftAll(){
varselectSrc=document.getElementById("select2");
varselectDes=document.getElementById("select1");
selAll(selectSrc,selectDes);
}
</script>
</head>
<body>
<selectid="select1"multiple="multiple"style="float:left;width:100px;height:200px;">
<option>添加</option>
<option>删除</option>
<option>修改</option>
<option>保存</option>
<option>查询</option>
</select>
<divstyle="float:left;width:50px;">
<inputtype="button"style="float:left;width:100%;"value=">"onclick="selectToRight()"/>
<inputtype="button"style="float:left;width:100%;"value="<"onclick="selectToLeft()"/>
<inputtype="button"style="float:left;width:100%;"value=">>"onclick="selectToRightAll()"/>
<inputtype="button"style="float:left;width:100%;"value="<<"onclick="selectToLeftAll()"/>
</div>
<selectid="select2"multiple="multiple"style="float:left;width:100px;height:200px"></select>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。