下拉列表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)文章
JavaScript代碼模擬鼠標(biāo)自動點擊事件示例
這篇文章主要介紹了JavaScript代碼模擬鼠標(biāo)自動點擊事件示例,文章通過示例代碼和運行效果圖介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
javascript設(shè)計模式之Adapter模式【適配器模式】實現(xiàn)方法示例
這篇文章主要介紹了javascript設(shè)計模式之Adapter模式,結(jié)合實例形式分析了JS適配器模式的原理與具體實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下2017-01-01

