我是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

          恩 不錯啊   回復  更多評論   

          主站蜘蛛池模板: 湘潭市| 双鸭山市| 汝州市| 武强县| 苍溪县| 昌宁县| 延吉市| 得荣县| 宁南县| 沈丘县| 高唐县| 东辽县| 台湾省| 那坡县| 承德市| 阳朔县| 临城县| 理塘县| 兴城市| 宜宾市| 文水县| 安康市| 晋中市| 安阳市| 普洱| 白山市| 新乡市| 大足县| 大埔区| 佛学| 朔州市| 稷山县| 宁国市| 南阳市| 黔江区| 鄱阳县| 富裕县| 黄大仙区| 油尖旺区| 丹巴县| 新昌县|