在可编辑的div中,我通过某个事件触发后,在div中添加我想要的内容,但是光标我无法移动到最后一行
例如
<div id="main" contenteditable=true  style="margin:0 0 0 0;"></div>
var rng = document.body.createTextRange();
rng.moveToPoint(rng.boundingLeft+10,rng.boundingHeight);
        rng.select();
之前我是想通过div的rng.boundingHeight做的,当有内容增加的时候这个div的boundingHeight会增加
但是在出现滚动条的时候,光标就定位不到了希望梅老大指点下,如果你有更好的方法最好。

解决方案 »

  1.   

    可以先用 scrollIntoView 显示后 ,再定位
      

  2.   

    var odiv = document.getElementById("main");
    odiv.scrollTop = odiv.scrollHeight;
      

  3.   

    各位英雄朋友,帮帮小弟啊,
    梅老大不在,各位知道的研究一下问题很简单,我就是想光标在内容的最后一行就行
    textarea可以实现
    可编辑div不好做
      

  4.   

    写JS俺有一个原则,专属某某BS的东西不搞....
    这个是专属IE...
      

  5.   

    给你一个例子吧。你研究一下。
    这个是 以前写的 光标定位的问题!----------------------------------------------------------------------
    <html>
    <head><title>光标定位</title>
    <script language="javascript">
      function getCursor(obj){    var curSelection=document.selection;    //选取对象
        
    var selectionRange=curSelection.createRange(); //为选取的对象创建文本 
    // alert("选取对象的长度:"+selectionRange.text.length);
        var bakRange=curSelection.createRange();       //为拖选的对象创建文本
    // alert(bakRange.text.length);    obj.select();  
        var allRange=document.selection.createRange();
          
        var positionNode=document.getElementById("position"); //取出文本框的ID
    // alert(positionNode.value);
        selectionRange.setEndPoint("StartToStart",allRange);//光标向左移动bakRange.text所返回来的位置
        
    //  selectionRange.moveStart("character",-bakRangeRange.text.length);    //把字符"character"向左移动-100000个单位.
     
    selectionRange.setEndPoint("EndToStart",bakRange); //在以前的结束位置文档和新文档开始位置之间创建对象.
        selectionRange.select();
    alert("查找字符串返回的长度:"+selectionRange.text.length);

        beginCursor = selectionRange.text.length;   //起始的光标位置.    positionNode.value="选中起始坐标:"+selectionRange.text.length+". "; //把起始的坐标显示到文本框中
        
        selectionRange.setEndPoint("EndToEnd",bakRange);        //在以前文档的结束位置和新文档的结束位置创建对象  
        selectionRange.select();                                 //显示光标 
        
        positionNode.value+="选中终止坐标:"+selectionRange.text.length+". "; //把结束的坐标显示到文本框中
        
    endCursor = selectionRange.text.length;    //结束的光标位置.     
    str1 = obj.value.substring(beginCursor,endCursor);
        positionNode.value+="鼠标拖曳文字选中的字符串:"+str1+". "; //把结束的坐标显示到文本框中
        bakRange.select();
       
      }
    </script>
    </head>
    <body >
    <textarea cols="80" rows="30" onmouseup="getCursor(this)" id="textarea">
    aabbdafadsfasdfasdfasdfsadfas[fadsfad]dfasdfasd</textarea>
    <br>
    插入位置:<input type="text" size="100" id="position"></input>
    </body>
    </html>
    -------------------------------------------------------------------------------------------------
      

  6.   

    我先看看,
    最好给个div的,textArea我已经实现了textArea的id,name是box
    function movePoint() 

    var rng = box.createTextRange(); rng.moveStart("character",rng.text.length); rng.collapse(true); rng.select(); }
    但是div没有 createTextRange();这个方法啊