本文实例讲述了javascript控制listbox列表框的项目上下移动的方法。分享给大家供大家参考。具体分析如下:
这段js代码可以控制listbox内的元素向上或者向下移动,这个功能非常有用。下面是详细的代码
复制代码 代码如下:
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) (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
下面是详细的演示代码,可以在浏览器内使用
复制代码 代码如下:
click below buttons to select or deselect all options from select box.
india
united states
china
italy
germany
canada
france
united kingdom
move up
move down
希望本文所述对大家的javascript程序设计有所帮助。