http://expert.csdn.net/Expert/topic/942/942792.xml?temp=.1446039

解决方案 »

  1.   

    根据大侠们的提示,下面为在一个TextArea中查找文字,可上下查询
    var  IE4 = (document.all);
    var  win = window;
    var  n = 0;

    function T_Check_Changed()
    {
    n = 0;
    }

    function findInPage(str, num, type)
    {
    //type = 0 means find down,type = 1 means find up
    var txt, i, found;
     
    if (IE4) 
    {
    txt = window.document.f_Journal.T_Journal.createTextRange();
    for (i = 0; i <= num && (found = txt.findText(str)) != false; i++) 
    {
    txt.moveStart("character", 1);
    txt.moveEnd("textedit");
    }
    if (found) 
    {
    txt.moveStart("character", -1);
    txt.findText(str);
    txt.scrollIntoView();
    txt.select();
    txt.moveEnd("textedit");
    if (type == 0)
    n++;
    else
    n--;
    return true;
    }
    else 
    {
    return false;
    }
    }//end of IE4
    }

    function findInPageDown(str) 
    {
    var ret;  

    if (str == "")
    {
    alert("検索文字はNULLです!");
    return false;
    }

    ret = findInPage(str, n, 0);
    if (ret == false)
    {
    n = 0;
    ret = findInPage(str, n, 0);
    if (ret == false)
    {
    alert("検索文字は見せてません!");
    }
    }

    return true;
    }
    function findInPageUp(str) 
    {
    var ret;
    var txt, i, found;

    if (str == "")
    {
    alert("検索文字はNULLです!");
    return false;
    }

    txt = window.document.f_Journal.T_Journal.createTextRange();
    if (n > 1)
    {
    ret = findInPage(str, n-2, 1);
    }
    else
    {  
    n = 0;
    for (i = 0;(found = txt.findText(str)) != false; i++)
    {
    txt.moveStart("character", 1);
    txt.moveEnd("textedit");
    n++;
    }
    if (n > 0)
    {
    txt.moveStart("character", -1);
    txt.findText(str);
    txt.scrollIntoView();
    txt.select();
    txt.moveEnd("textedit");
    return true;
    }
    else
    {
    alert("検索文字は見せてません!");
    }
    }
    return true;
    }