js 如果获取光标的位置。(在显示器中的位置)光标,不是鼠标。(其实就是textbox焦点)

解决方案 »

  1.   

    <textarea   id=show   rows=10   cols=100>   
      先将光标置在这里任意处,然后点击按钮,看光标变化   
      </textarea>   
      <br><input   type=button   value=取得当前坐标   onclick='window.status=GetCursorPos()'>   
      <script>   
      function   GetCursorPos(oTextArea)     
      {     
      s="~!@#$%^";   
      clipboardData.setData('text',s);     
      show.focus();     
      document.execCommand('paste');     
      var   arr=show.value.split(s);     
      show.value=arr[1];   
      show.document.selection.empty();   
      show.document.selection.createRange().select();   
      show.focus();   
      clipboardData.setData('text',arr[0]);     
      document.execCommand('paste');     
      alert(arr[0].length);     
      }   
      </script>  
      

  2.   

    <input id=txt1 value="oh,my god">
    <input type=button onclick="getPosition();">
    <script type="text/jscript">
    function getPosition() {
        var obj = document.getElementById("txt1");    obj.focus();
        var range = document.selection.createRange();    range.moveStart("textedit",-1);
        alert("position:"+range.text.length);
    }</script>
      

  3.   


    可以这样理解,有什么好的解决办法吗? 以前div是跟着鼠标走的。现在要div跟着光标走。
      

  4.   

    <script language="javascript">
    function tellPoint()
    {
    var rng = event.srcElement.createTextRange();
    rng.moveToPoint(event.x,event.y);
    rng.moveStart("character",-event.srcElement.value.length);
    pnum.value = rng.text.length;
    }
    </script>