我是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 衡鋒 閱讀(3081) 評論(1)  編輯  收藏 所屬分類: javascriptWeb開發

          評論

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

          恩 不錯啊   回復  更多評論   

          主站蜘蛛池模板: 久治县| 拉萨市| 德惠市| 电白县| 松江区| 郁南县| 阳谷县| 绍兴县| 来安县| 灌云县| 达拉特旗| 铜陵市| 五寨县| 策勒县| 汉川市| 张家川| 邻水| 疏勒县| 广宗县| 绍兴县| 连州市| 那曲县| 邯郸县| 盱眙县| 铜鼓县| 永丰县| 双流县| 得荣县| 望城县| 霞浦县| 福清市| 临汾市| 定襄县| 陆良县| 成武县| 潞城市| 云安县| 桐乡市| 高雄市| 仙桃市| 沁水县|