国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript控制listbox列表框的項目上下移動的方法

2019-11-20 12:54:11
字體:
來源:轉載
供稿:網友

本文實例講述了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) < 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

下面是詳細的演示代碼,可以在瀏覽器內使用
復制代碼 代碼如下:
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>

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平塘县| 新宁县| 余姚市| 泸定县| 瓮安县| 建平县| 樟树市| 巴马| 长岭县| 丰城市| 胶州市| 潞城市| 登封市| 曲靖市| 桃源县| 保德县| 九台市| 隆安县| 桂阳县| 长兴县| 长春市| 娄底市| 白沙| 车险| 德化县| 汉阴县| 蕉岭县| 全州县| 衡东县| 剑河县| 高陵县| 延安市| 商水县| 丹江口市| 中超| 晴隆县| 莫力| 双桥区| 东辽县| 应城市| 武定县|