本文實例講述了javascript基于DOM實現權限選擇的方法。分享給大家供大家參考。具體實現方法如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>權限選擇</title>
<script type="text/javascript">
//====================多選操作====================================
function selMultiple(selectSrc, selectDes) {
for (var i = selectSrc.childNodes.length - 1; i >= 0; i--) {
var option = selectSrc.childNodes[i];
if (option.selected == true) {
selectSrc.removeChild(option);
option.selected = false;
selectDes.appendChild(option);
}
}
}
function selectToRight() {
var selectSrc = document.getElementById("select1");
var selectDes = document.getElementById("select2");
selMultiple(selectSrc, selectDes);
}
function selectToLeft() {
var selectSrc = document.getElementById("select2");
var selectDes = document.getElementById("select1");
selMultiple(selectSrc, selectDes);
}
//====================全選操作====================================
function selAll(selectSrc, selectDes) {
// 這種寫法有問題,發現selectSrc.childNodes.length居然等于10,實際上只有5個元素
// for (var i = 0; i < selectSrc.childNodes.length; i++) {
// var option = selectSrc.childNodes[0];
// selectSrc.removeChild(option);
// selectDes.appendChild(option);
// }
var options = selectSrc.getElementsByTagName("option");
var optLength = options.length;
/*
注意:for循環中不能直接使用options.length,因為selectDes.appendChild執行后
會導致options.length減一,所以先把options.length存放到一個變量中備用
*/
for (var i = 0; i < optLength; i++) {
var option = options[0]; //這里使用的始終是第0個元素
selectDes.appendChild(option);
}
selectSrc.options.length = 0;
}
function selectToRightAll() {
var selectSrc = document.getElementById("select1");
var selectDes = document.getElementById("select2");
selAll(selectSrc, selectDes);
}
function selectToLeftAll() {
var selectSrc = document.getElementById("select2");
var selectDes = document.getElementById("select1");
selAll(selectSrc, selectDes);
}
</script>
</head>
<body>
<select id="select1" multiple="multiple" style="float:left;width:100px;height:200px;">
<option>添加</option>
<option>刪除</option>
<option>修改</option>
<option>保存</option>
<option>查詢</option>
</select>
<div style="float:left;width:50px;">
<input type="button" style="float:left;width:100%;" value=">" onclick="selectToRight()" />
<input type="button" style="float:left;width:100%;" value="<" onclick="selectToLeft()" />
<input type="button" style="float:left;width:100%;" value=">>" onclick="selectToRightAll()" />
<input type="button" style="float:left;width:100%;" value="<<" onclick="selectToLeftAll()" />
</div>
<select id="select2" multiple="multiple" style="float:left;width:100px;height:200px"></select>
</body>
</html>
希望本文所述對大家的javascript程序設計有所幫助
新聞熱點
疑難解答