下拉列表select 由左邊框移動到右邊示例
更新時間:2013年12月04日 17:17:45 作者:
select由左邊框移動到右邊,下面有個不錯的示例,大家可以參考下
當(dāng)頁面還沒有加載完的時候調(diào)用下面語句,會取不到“add” 這個對象,提示為空或不是對象
document.getElementById("add").onclick = function(){
alert("hello");
}
當(dāng)使用便可取的對象
window.onload = function(){
document.getElementById("add").onclick = function(){
alert("hello");
}
}
<script type="text/javascript">
//選中的從左邊移到右邊
function toright() {
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
var firstoptionElement = firstElement.getElementsByTagName("option");
var len = firstoptionElement.length;
for(var i=0;i<len;i++){
if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的屬性
secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]);
}
}
}
//全部移動到右邊
function allright(){
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
var firstoptionElement = firstElement.getElementsByTagName("option");
var len = firstoptionElement.length;
for(var i=0;i<len;i++){
secondElement.appendChild(firstoptionElement[0]);//option選項選中時候索引為0
}
}
//雙擊移動到右邊
function db(){
/* //方法一
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
var firstoptionElement = firstElement.getElementsByTagName("option");
var len = firstoptionElement.length;
for(var i=0;i<len;i++){
if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的屬性
secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]);
}
} */
//方法二
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
secondElement.appendChild(firstElement[firstElement.selectedIndex]);
}
</script>
<style type="text/css">
</style>
</head>
<body>
<table width="285" height="169" border="0" align="left">
<tr>
<td width="126">
<select name="first" size="10" multiple="multiple" id="first" ondblclick="db()">
<option value="1">選項1</option>
<option value="2">選項2</option>
<option value="3">選項3</option>
<option value="4">選項4</option>
<option value="5">選項5</option>
<option value="6">選項6</option>
</select>
</td>
<td width="69" valign="middle">
<input id="add" name="add" type="button" value="---->" onclick="toright()"/>
<input id="add_all" name="add_all" type="button" value="==>" onclick="allright()"/>
</td>
<td width="127" align="left">
<select name="second" size="10" multiple="multiple" id="second">
<option value="選項8">選項8</option>
</select>
</td>
</tr>
</table>
</body>
復(fù)制代碼 代碼如下:
document.getElementById("add").onclick = function(){
alert("hello");
}
當(dāng)使用便可取的對象
復(fù)制代碼 代碼如下:
window.onload = function(){
document.getElementById("add").onclick = function(){
alert("hello");
}
}
復(fù)制代碼 代碼如下:
<script type="text/javascript">
//選中的從左邊移到右邊
function toright() {
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
var firstoptionElement = firstElement.getElementsByTagName("option");
var len = firstoptionElement.length;
for(var i=0;i<len;i++){
if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的屬性
secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]);
}
}
}
//全部移動到右邊
function allright(){
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
var firstoptionElement = firstElement.getElementsByTagName("option");
var len = firstoptionElement.length;
for(var i=0;i<len;i++){
secondElement.appendChild(firstoptionElement[0]);//option選項選中時候索引為0
}
}
//雙擊移動到右邊
function db(){
/* //方法一
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
var firstoptionElement = firstElement.getElementsByTagName("option");
var len = firstoptionElement.length;
for(var i=0;i<len;i++){
if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的屬性
secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]);
}
} */
//方法二
var firstElement = document.getElementById("first");
var secondElement = document.getElementById("second");
secondElement.appendChild(firstElement[firstElement.selectedIndex]);
}
</script>
<style type="text/css">
</style>
</head>
<body>
<table width="285" height="169" border="0" align="left">
<tr>
<td width="126">
<select name="first" size="10" multiple="multiple" id="first" ondblclick="db()">
<option value="1">選項1</option>
<option value="2">選項2</option>
<option value="3">選項3</option>
<option value="4">選項4</option>
<option value="5">選項5</option>
<option value="6">選項6</option>
</select>
</td>
<td width="69" valign="middle">
<input id="add" name="add" type="button" value="---->" onclick="toright()"/>
<input id="add_all" name="add_all" type="button" value="==>" onclick="allright()"/>
</td>
<td width="127" align="left">
<select name="second" size="10" multiple="multiple" id="second">
<option value="選項8">選項8</option>
</select>
</td>
</tr>
</table>
</body>
相關(guān)文章
關(guān)于在IE下的一個安全BUG --可用于跟蹤用戶的系統(tǒng)鼠標(biāo)位置
本篇文章小編將為大家介紹,關(guān)于在IE下的一個安全BUG --可用于跟蹤用戶的系統(tǒng)鼠標(biāo)位置。需要的朋友可以參考一下2013-04-04Bootstrap企業(yè)網(wǎng)站實戰(zhàn)項目4
這篇文章主要為大家分享了Bootstrap企業(yè)網(wǎng)站實戰(zhàn)項目,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Bootstrap基本插件學(xué)習(xí)筆記之Popover提示框(19)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本插件學(xué)習(xí)筆記之Popover提示框的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Vue項目vscode 安裝eslint插件的方法(代碼自動修復(fù))
這篇文章主要介紹了Vue項目vscode 安裝eslint插件的方法 代碼自動修復(fù),需要的朋友可以參考下2020-04-04