Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          <html>
          <head>
          <title>TEST</title>
          <style>
          body,td{
              font-family: verdana, arial, helvetica, sans-serif;
              font-size: 12px;
          }
          </style>
          <script type="text/javascript">
              var start=0;
              var end=0;
              function add(){       
                  var textBox = document.getElementById("ta");
                  var pre = textBox.value.substr(0, start);
                  var post = textBox.value.substr(end);
                  textBox.value = pre + document.getElementById("inputtext").value + post;
              }
              function savePos(textBox){
                  //如果是Firefox(1.5)的話,方法很簡單
                  if(typeof(textBox.selectionStart) == "number"){
                      start = textBox.selectionStart;
                      end = textBox.selectionEnd;
                  }
                  //下面是IE(6.0)的方法,麻煩得很,還要計算上'"n'
                  else if(document.selection){
                      var range = document.selection.createRange();
                      if(range.parentElement().id == textBox.id){
                          // create a selection of the whole textarea
                          var range_all = document.body.createTextRange();
                          range_all.moveToElementText(textBox);
                          //兩個range,一個是已經(jīng)選擇的text(range),一個是整個textarea(range_all)
                          //range_all.compareEndPoints()比較兩個端點(diǎn),如果range_all比range更往左(further to the left),則                //返回小于0的值,則range_all往右移一點(diǎn),直到兩個range的start相同。
                          // calculate selection start point by moving beginning of range_all to beginning of range
                          for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
                              range_all.moveStart('character', 1);
                          // get number of line breaks from textarea start to selection start and add them to start
                          // 計算一下"n
                          for (var i = 0; i <= start; i ++){
                              if (textBox.value.charAt(i) == '\n')
                                  start++;
                          }
                          // create a selection of the whole textarea
                           var range_all = document.body.createTextRange();
                           range_all.moveToElementText(textBox);
                           // calculate selection end point by moving beginning of range_all to end of range
                           for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end ++)
                               range_all.moveStart('character', 1);
                               // get number of line breaks from textarea start to selection end and add them to end
                               for (var i = 0; i <= end; i ++){
                                   if (textBox.value.charAt(i) == '\n')
                                       end ++;
                               }
                          }
                      }
                  document.getElementById("start").value = start;
                  document.getElementById("end").value = end;
              }
          </script>
          </head>
          <body>
          <form action="a.cgi">
          <table border="1" cellspacing="0" cellpadding="0">
              <tr>
                  <td>start: <input type="text" id="start" size="3"/></td>
                  <td>end: <input type="text" id="end" size="3"/></td>
              </tr>
              <tr>
                  <td colspan="2">
                      <textarea id="ta" onKeydown="savePos(this)"
                                        onKeyup="savePos(this)"
                                        onmousedown="savePos(this)"
                                        onmouseup="savePos(this)"
                                        onfocus="savePos(this)"
                                        rows="14" cols="50"></textarea>
                  </td>
              </tr>
              <tr>
                  <td><input type="text" id="inputtext" /></td>
                  <td><input type="button" onClick="add()" value="Add Text"/></td>
              </tr>
          </table>
          </form>
          </body>
          </html>

          附:IE 中的定義: http://msdn.microsoft.com/en-us/library/ms535872%28VS.85%29.aspx
          http://msdn.microsoft.com/en-us/library/ms533042%28VS.85%29.aspx
          posted on 2010-06-08 22:23 seal 閱讀(1391) 評論(0)  編輯  收藏 所屬分類: Javascript
          主站蜘蛛池模板: 玛曲县| 临洮县| 莎车县| 华池县| 洪洞县| 永定县| 汽车| 沅陵县| 颍上县| 腾冲县| 澎湖县| 饶阳县| 宜兴市| 巴塘县| 炉霍县| 湘乡市| 兰西县| 怀安县| 额敏县| 玛多县| 郁南县| 丹巴县| 五常市| 卓尼县| 获嘉县| 朝阳县| 唐山市| 沾益县| 安龙县| 孟津县| 银川市| 上蔡县| 堆龙德庆县| 兰溪市| 丰顺县| 辛集市| 乌拉特后旗| 罗源县| 沅陵县| 祁门县| 吉首市|