繁体   English   中英

如何突出显示javascript列表框项目?

[英]How do I highlight a javascript listbox item?

我有一个函数,它可以将选择框(列表框)中的选定项目与其上方的项目交换,可以正常工作,但我想使其选中,以便仍选择后置项目。 因此,如果用户希望继续在框中向上移动项目,则可以继续按下“上移”按钮。

    function moveUp() {
        var list = document.getElementById('listbox');
        var numSelected = list.selectedIndex;
        var itemSelected = list.options;            

        if (itemSelected[numSelected].id == 0) {
            alert("Can't move this up the list!");
        } else {
            if (poiArrayList[numSelected - 1] != null) {
                var tempPOI = poiArrayList[numSelected];
                poiArrayList[numSelected] = poiArrayList[numSelected - 1];
                poiArrayList[numSelected - 1] = tempPOI;
                //The line below is what I have but that doesn't seem to work.
                list.selectedIndex = numSelected;
            } else {
                alert("The listbox is empty!");
            }
        }
    }

完整代码

在这里看看: http : //jsfiddle.net/2ae9B/1/

我在generateListBox()添加了一个可选参数,您可以在其中设置生成列表后突出显示的索引。 例如:

function moveUp() {
    var list = document.getElementById('listbox');
    var numSelected = list.selectedIndex;
    ...
    ...

    // regenerate the list passing the item to select
    generateListBox(numSelected - 1);
}

function generateListBox(selectedIndex) {
    var selectBox = document.getElementById("listbox");
    selectBox.innerHTML = "";
    for (var i = 0; i < poiArrayList.length; i++) {
        lbAddItem(poiArrayList[i].name, i);
    }

    // you should also check that is a valid integer here
    if(selectedIndex)
        selectBox.selectedIndex = selectedIndex;
}

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM