JS函数在FF和Safari中运行良好,但在IE9中需要很长时间

时间:2011-11-02 17:25:36

标签: javascript

你能看到它在FF和Safari中运行正常的原因,但是当它在IE9中运行时它会完全崩溃吗?它需要永远,几乎挂起浏览器。谢谢!

这是一个在按键后刷新列表的功能。该函数清除列表,然后通过全局定义的数组进行线性搜索,并将匹配项添加回列表。

function handleKeyUp()
{
    var selectObj, textObj, componentListLength;
    var i, searchPattern, numShown;

    // Set references to the form elements
    selectObj = document.form1.componentselect;
    textObj = document.form1.componentinput;

    // Remember the function list length for loop speedup
    componentListLength = componentlist.length;

    // Set the search pattern depending
    if(document.form1.componentradio[0].checked == true)
    {
        searchPattern = "^"+textObj.value;
    }
    else
    {
        searchPattern = textObj.value;
    }

    // Create a regular expression
    re = new RegExp(searchPattern,"gi");
    // Clear the options list
    selectObj.length = 0;

    // Loop through the array and re-add matching options
    numShown = 0;
    for(i = 0; i < componentListLength; i++)
    {
        if(componentlist[i].search(re) != -1)
        {
            selectObj[numShown] = new Option(componentlist[i],"");
            numShown++;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

两件看起来很可疑的东西:

// Clear the options list
selectObj.length = 0;
// should be
selectObj.options.length = 0;


// Next thing, adding them back
selectObj[numShown] = new Option(componentlist[i],"");
// should be
selectObj.options[numShown] = new Option(componentlist[i],"");