0
我有一個函數,當用戶選擇一個或多個項目時,它將這些項目移動到列表框的頂部(仍然選中)並滾動到頂部,重新可見。它適用於我測試過的所有主流瀏覽器。問題來了,當我有這個函數在表格單元格內排序列表框。當我這樣做並使用IE8時,它會正確排序&高光,但不會滾動到頂部。 JavaScript通過基本的JSLint檢查,網頁通過HTML驗證。我有沒有發現IE8的怪癖?我編碼這個listbox排序&滾動功能錯誤的方式?Javascript排序列表框工作除非在表中IE8
<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>
在其他的瀏覽器,他們向上滾動到列表頂部時所選擇的選項代碼集的最後一位。我發誓我在某個時候嘗試了scrollTop,但沒有運氣,但現在它可以工作......不能與此爭論。謝謝。也感謝鏈接到jsfiddle。這是一個非常有用的網站。 – Curtis 2010-03-23 14:38:50