我是FE,也是Fe

          前端來源于不斷的點滴積累。我一直在努力。

          統計

          留言簿(15)

          閱讀排行榜

          評論排行榜

          javascript獲取/設置 文本框/文本域中的光標位置

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
          <html>
              
          <head>
              
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> 
              
          <title>javascript獲取/設置 文本框/文本域中的光標位置</title>
              
          <meta name="Keywords" content=""/>
              
          <meta name="Description" content=""/>
              
          <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
              
          <script type="text/javascript">
                  
          function getTxt1CursorPosition(){
                      
          var oTxt1 = document.getElementById("txt1");
                      
          var cursurPosition=-1;
                      
          if(oTxt1.selectionStart){//非IE瀏覽器
                          cursurPosition= oTxt1.selectionStart;
                      }
          else{//IE
                          var range = document.selection.createRange();
                          range.moveStart(
          "character",-oTxt1.value.length);
                          cursurPosition
          =range.text.length;
                      }
                      alert(cursurPosition);
                  }

                  
          function setTxt1CursorPosition(i){
                      
          var oTxt1 = document.getElementById("txt2");
                      
          var cursurPosition=-1;
                      
          if(oTxt1.selectionStart){//非IE瀏覽器
                          oTxt1.selectionStart=i;
                      }
          else{//IE
                          var range = oTxt1.createTextRange();
                          range.move(
          "character",i);
                          range.select();
                      }
                  }

                  
          function getTa1CursorPosition(){
                      
          var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0];
                      
          var oTa1 = document.getElementById("ta1");
                      
          var cursurPosition=-1;
                      
          if(oTa1.selectionStart){//非IE瀏覽器
                          cursurPosition= oTa1.selectionStart;
                      }
          else{//IE
                          var range = oTa1.createTextRange();
                          range.moveToPoint(evt.x,evt.y);
                          range.moveStart(
          "character",-oTa1.value.length);
                          cursurPosition
          =range.text.length;
                      }
                      alert(cursurPosition);
                  }

                  
          function setTa1CursorPosition(i){
                      
          var oTa2 = document.getElementById("ta2");
                      
          if(oTa2.selectionStart){//非IE瀏覽器
                          oTa2.selectionStart=i;
                          oTa2.selectionEnd
          =i;
                      }
          else{//IE
                          var range = oTa2.createTextRange();
                          range.move(
          "character",i);
                          range.select();
                      }
                  }
              
          </script>
              
          </head>

          <body>
          <input type="text" id="txt1" name="" value="點擊我獲取光標位置" onkeyup="getTxt1CursorPosition()" onclick="getTxt1CursorPosition()"  />

          <input type="text" id="txt2" name="" value="點擊我設置光標位置為5" onclick="setTxt1CursorPosition(5)" />
          <hr />
          <textarea id="ta1" rows="" cols="" style="width:100%; height:90px;" onclick="getTa1CursorPosition()">abcdefg
          hijklmn
          opqrst
          uvwxyz
          點擊我獲取文本域的光標位置
          </textarea>

          <textarea id="ta2" rows="" cols="" style="width:100%; height:90px;" onclick="setTa1CursorPosition(30)">abcdefg
          hijklmn
          opqrst
          uvwxyz
          點擊我設置文本域的光標位置為30
          </textarea>
          </body>
          </html>

          posted on 2010-09-27 17:36 衡鋒 閱讀(3085) 評論(1)  編輯  收藏 所屬分類: javascriptWeb開發

          評論

          # re: javascript獲取/設置 文本框/文本域中的光標位置 2011-09-20 08:37 tb

          恩 不錯啊   回復  更多評論   

          主站蜘蛛池模板: 两当县| 吐鲁番市| 铜陵市| 井研县| 永丰县| 彭阳县| 襄樊市| 军事| 舟山市| 渑池县| 饶河县| 赣榆县| 个旧市| 新邵县| 成都市| 铜鼓县| 温州市| 河北区| 大兴区| 永州市| 霍山县| 手机| 登封市| 屯留县| 子长县| 宣恩县| 将乐县| 简阳市| 吉林市| 聂拉木县| 上栗县| 六枝特区| 平乡县| 宜阳县| 巫山县| 什邡市| 重庆市| 那曲县| 景泰县| 内丘县| 依兰县|