首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript排序列表框工作,除非在表IE8中

Javascript排序列表框工作,除非在表IE8中
EN

Stack Overflow用户
提问于 2010-03-23 22:02:11
回答 1查看 905关注 0票数 0

我有一个功能,当用户选择一个或多个项目时,它会将这些项目移动到列表框的顶部(仍处于选中状态),并滚动到顶部,以便它们可见。它可以在我测试过的所有主流浏览器中运行。当我使用此函数对表格单元格内的列表框进行排序时,问题就来了。当我这样做并使用IE8时,它会正确地排序和突出显示,但不会滚动到顶部。JavaScript通过基本的JSLint检查,网页通过HTML验证。我是不是发现了IE8的怪癖?我是不是写错了这个列表框排序和滚动函数?

代码语言:javascript
复制
<script type="text/javascript">
function Arrange(obj) {
    var countSel = 0, countNSel = 0, i, arr_selected = [], arr_unselected = [];
    for (i = 0; i <= obj.length - 1; i++) {
        if (obj.options[i].selected) {
            //create array with all selected items then sort it
            arr_selected[countSel] = [];
            arr_selected[countSel][0] = obj.options[i].text;
            arr_selected[countSel][1] = obj.options[i].value;
            countSel = countSel + 1;
            arr_selected.sort();
        }
        else {
            //create array with all UNselected items then sort it
            arr_unselected[countNSel] = [];
            arr_unselected[countNSel][0] = obj.options[i].text;
            arr_unselected[countNSel][1] = obj.options[i].value;
            countNSel = countNSel + 1;
            arr_unselected.sort();
        }
    }
    if (countSel !== 0) {
        //overwrite listbox options with selected items
        for (i = 0; i <= countSel - 1; i++) {
            obj.options[i] = new Option(arr_selected[i][0], arr_selected[i][1]);
        }
        //overwrite listbox options with UNselected items
        for (i = 0; i <= countNSel - 1; i++) {
            obj.options[countSel] = new Option(arr_unselected[i][0], arr_unselected[i][1]);
            countSel = countSel + 1;
        }
    }

    //for showing selected items
    //scroll to the top of the list and select the appropriate items
    //it's in reverse order because IE8 wanted to only scroll to the last item selected
    obj.selectedIndex = 0;
    for (i = arr_selected.length - 1; i >= 0; i--) {
        obj.options[i].selected = true;
    }
}
</script>
</head>
<body>
<form name="frm" action="post">
<table><tr><td>test:</td><td>
<select size="4" name="cbostaffcontact" multiple onblur="Arrange(document.forms[0].cbostaffcontact);">
<option value="user1">Al</option>
<option value="user2">Bob</option>
<option value="user3">Christy</option>
<option value="user4">Jane</option>
<option value="user5">Randy</option>
<option value="user6">Tom</option>
<option value="user7">Zed</option>
</select>
</td></tr></table>
</form>
EN

Stack Overflow用户

回答已采纳

发布于 2010-03-23 22:25:08

这可能是IE8的一个怪癖,但你的代码实际上并没有说任何关于滚动的事情,所以我不会怪IE8。下面是一个应该solve your problem的简单指令

代码语言:javascript
复制
obj.scrollTop=0;
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2500476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档