JavaScript控制listbox列表框的項目上下移動的方法
更新時間:2015年03月18日 11:49:34 作者:上大王
這篇文章主要介紹了JavaScript控制listbox列表框的項目上下移動的方法,實例分析了javascript操作listbox列表框的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了JavaScript控制listbox列表框的項目上下移動的方法。分享給大家供大家參考。具體分析如下:
這段JS代碼可以控制listbox內(nèi)的元素向上或者向下移動,這個功能非常有用。下面是詳細的代碼
復制代碼 代碼如下:
function listbox_move(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
//..
//..
listbox_move('countryList', 'up'); //move up the selected option
listbox_move('countryList', 'down'); //move down the selected option
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
//..
//..
listbox_move('countryList', 'up'); //move up the selected option
listbox_move('countryList', 'down'); //move down the selected option
下面是詳細的演示代碼,可以在瀏覽器內(nèi)使用
復制代碼 代碼如下:
Click below buttons to select or deselect all options from select box.<br>
<select id="lsbox" name="lsbox" size="10" multiple="">
<option value="1">India</option>
<option value="2">United States</option>
<option value="3">China</option>
<option value="4">Italy</option>
<option value="5">Germany</option>
<option value="6">Canada</option>
<option value="7">France</option>
<option value="8">United Kingdom</option>
</select> <br>
<button onclick="listboxMove('lsbox', 'up');">Move Up</button>
<button onclick="listboxMove('lsbox', 'down');">Move Down</button>
<script>
function listboxMove(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
</script>
<select id="lsbox" name="lsbox" size="10" multiple="">
<option value="1">India</option>
<option value="2">United States</option>
<option value="3">China</option>
<option value="4">Italy</option>
<option value="5">Germany</option>
<option value="6">Canada</option>
<option value="7">France</option>
<option value="8">United Kingdom</option>
</select> <br>
<button onclick="listboxMove('lsbox', 'up');">Move Up</button>
<button onclick="listboxMove('lsbox', 'down');">Move Down</button>
<script>
function listboxMove(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
</script>
希望本文所述對大家的javascript程序設計有所幫助。
您可能感興趣的文章:
相關文章
基于JavaScript實現(xiàn)移動端點擊圖片查看大圖點擊大圖隱藏
最近接了個項目,項目需求是這樣的,當點擊圖片查看圖片,再次點擊大圖被隱藏,在移動端用的比較多,因為移動端屏幕小,需要查看大圖。具體代碼實現(xiàn)過程本文給大家介紹,需要的朋友可以參考下2015-11-11JavaScript中的call和apply的用途以及區(qū)別
本文主要介紹了JavaScript中的call和apply的用途以及區(qū)別。具有很好的參考價值,下面跟著小編一起來看下吧2017-01-01javascript Slip.js實現(xiàn)整屏滑動的手機網(wǎng)頁
Slip.js能做什么?Slip.js可以讓你的手機網(wǎng)站像原生手機軟件一樣慣性滾動,手觸圖片輪換等等,對Slip.js感興趣的小伙伴們可以參考一下2015-11-11addEventListener()與removeEventListener()解析
這篇文章主要為大家詳細介紹了addEventListener()與removeEventListener(),用于處理指定和刪除事件處理程序操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04bootstrap時間控件daterangepicker使用方法及各種小bug修復
這篇文章主要介紹了bootstrap時間控件daterangepicker使用方法,及各種小bug修復,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10