本帖最后由 huangwenquan123 于 2012-05-22 09:16:13 编辑

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <style>
    html,body{width:100%;height:100%}
    </style>
    </head>
    <body>
    <div style="width:400px;height:2000px;background-color:#F00;float:right">
    <div></div>
    </div>
    <div style="width:400px; height:200px; background-color:#F00"></div>
    <div style="width:400px; height:200px; background-color:#F00;float:right"></div>
    <input type="button"></input>
    <input type="text"></input>
    <textarea id='txtText' cols=50 rows=6>









    </textarea></body>
    <script>var txt = "时";
    var txtText = document.getElementById('txtText');
    var start = txtText.value.replace(/\r\n/ig,'$').indexOf(txt); // IE 换行符 \r\n 需要处理一下
    var end = start + txt.length;
    setSelectRange(txtText,start,end);  /**
    * 设置textarea的选中区域
    */
     function setSelectRange( textarea, start, end )
    {
    if ( typeof textarea.createTextRange != 'undefined' )// IE
    {
    var range = textarea.createTextRange();
    // 先把相对起点移动到0处
    range.moveStart( "character", 0)
    range.moveEnd( "character", 0);
    range.collapse( true); // 移动插入光标到start处
    range.moveEnd( "character", end);
    range.moveStart( "character", start);
    range.select();
    } // if
      else if ( typeof textarea.setSelectionRange != 'undefined' )
     {
       textarea.setSelectionRange(start, end);
       textarea.focus();
     } // else

    window.onload=function(e){bindEvent(document.body,'click',function(e){
    e=e||event;
    var obj = e.srcElement || e.target;
    if(typeof(obj.innerHTML)=='string'){
    var div=document.createElement("div");
    var x=e.x||e.pageX;
    var y=e.y||e.pageY;
    div.style.cssText="position:absolute;left:"+x+"px;top:"+y+"px;";
    div.innerHTML="Good";
    document.body.appendChild(div);
    }
    });
    };function bindEvent(obj,evt,fun) 
    {
         if(window.addEventListener){
               obj.addEventListener(evt, function(e){ fun(e);},false); 
         }else{
               obj.attachEvent('on'+evt,fun);     
        }
    }
    var xxx = {a:{a1:"a1"},b:{b1:{b11:1,b12:2}}};
    var yyy = {b:{b1:{b11:4,b12:5}}};
    for(var y in yyy)
    {
    xxx[y] = yyy[y];
    }
    function getMatchs(str,tag){
    var reg =eval('/\\['+tag+'\\](.+?)\\[\\/'+tag+'\\]/i');
    return str.match(reg);
    }
    var m = getMatchs('aaaa[cn]中文[/cn]bbbb[en]英文[/en]ccccc','cn');
    //alert(m.length +"\r\n" +m[0]+"\r\n"+m[1]);</script>
    </html>
      

  2.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <style>
    html,body{width:100%;height:100%}
    </style>
    </head>
    <body>
    <textarea id='txtText' cols=50 rows=6>









    </textarea></body>
    <script>var txt = "时";
    var txtText = document.getElementById('txtText');
    var start = txtText.value.replace(/\r\n/ig,'$').indexOf(txt); // IE 换行符 \r\n 需要处理一下
    var end = start + txt.length;
    setSelectRange(txtText,start,end);  /**
    * 设置textarea的选中区域
    */
     function setSelectRange( textarea, start, end )
    {
    if ( typeof textarea.createTextRange != 'undefined' )// IE
    {
    var range = textarea.createTextRange();
    // 先把相对起点移动到0处
    range.moveStart( "character", 0)
    range.moveEnd( "character", 0);
    range.collapse( true); // 移动插入光标到start处
    range.moveEnd( "character", end);
    range.moveStart( "character", start);
    range.select();
    } // if
      else if ( typeof textarea.setSelectionRange != 'undefined' )
     {
       textarea.setSelectionRange(start, end);
       textarea.focus();
     } // else

    </script>
    </html>
      

  3.   

    帅,原来是ie多了个\r.害我纠结了一早上.
    我也刚好写好了,原本ie是用findText来处理,ff用selectionStart来处理.        var txtBox = document.getElementById("text_Area");
            var index=0;
            if(document.selection)
                 rng = txtBox.createTextRange();
            function findText(str){
                 if(str=="")
                    return;
                 str=str.toLowerCase();
                 if(document.selection){
                    var num = document.selection.createRange().text.length;
                     rng.moveStart("character",num);
                    rng.moveEnd("character",txtBox.value.length);
                       if(rng.findText(str))
                       rng.select();
                     if(rng.text.toLowerCase()!=str){   
                        alert("已到达文件结尾");
                        rng = txtBox.createTextRange();
                    }
                }
                else{
                    index = txtBox.value.toLowerCase().indexOf(str,index);
                    if(index==-1){
                        alert("已到达文件结尾");
                        return;
                    }
                    txtBox.selectionStart=index;
                    var end = index+str.length;
                    txtBox.selectionEnd=end;
                    txtBox.focus();
                    index++;
                }
            }
    最后还是用你那个方法.<body>
        <textarea id="txtText" cols="120" rows="20"></textarea>
        <input id="txtSearch" type="text" />
        <input id="Button1" type="button" value="搜索" onclick="Search()" />
    </body>
    <script type="text/javascript">
    var txtSearch = document.getElementById("txtSearch");
    var txtText = document.getElementById("txtText");
    var Start=0,End=0;
    function Search(){
        if(txtSearch.value.length==0){
            alert("请输入要搜索的字符!");
            return;
        }
        Start = txtText.value.toLowerCase().replace(/\r\n/ig,'\n').indexOf(txtSearch.value.toLowerCase(),Start);
        End = Start+txtSearch.value.length;
        if(Start==-1){
            alert("已到达文件结尾!");
            return;
        }
        if(document.selection){
             var range=txtText.createTextRange();
             range.collapse(true);
             range.moveEnd("character",End);
             range.moveStart("character",Start);
             range.select();
        }
        else{
           txtText.setSelectionRange(Start, End);
           txtText.focus();
        }
        Start++;
    }
    </script>