<DIV id="init" contentEditable=true style="FONT-SIZE:16px; OVERFLOW: auto; WIDTH: 604px; font-family: Arial, '宋体';border:solid 1px black;word-wrap:break-word;height:100px;cursor:text;" ><br _moz_dirty=""/></DIV>我要得到光标在这个DIV中的Index位置 请分别对火狐 和IE作出解释 在网上找了大半个晚上  有的只是针对textarea的解决方案  我现在用DIV做richEditor  不知道怎么得到光标位置  当输入文本的时候很方便  但是插入图片的时候   就不能将图片插入到当前光标所在位置  开心网留言板的Editor使用Iframe做的  他的就可以实现光标处插入图片  不知道他是怎么实现的  请不要说要我换成 Iframe或者其他控件 我目前的情况只能用DIV  谢谢 

解决方案 »

  1.   

    能否通过屏幕像素点进行计算呢 event.x+document.body.scrollLeft-divX
      

  2.   

    因为层没有加载完是不能获取到位置的。。当打开网页时,DIV的位置也自己去指定。这样才能取到层在页面中的位置
    <html>
    <head>
    <title>2</title>
    <style type="text/css">
    #area{width:300px;height:100px;border:1px solid #999;position:relative;margin-bottom:5px;}
    #area #col{width:200px;height:50px;border:1px solid #999;position:absolute;}
    </style>
    </head>
    <script type="text/javascript">
    window.onload=function(){
    document.getElementById("col").style.top="5px";
    document.getElementById("col").style.left="20px";
    }
    function aa(){
    alert("距顶:" + document.getElementById("col").style.top + "距左:" + document.getElementById("col").style.left);
    }
    </script>
    <body>
    <div id="area">
    <div id="col">aaa</div>
    </div>
    <input onclick="aa();" type="button" value="获值" />
    <script type="text/javscript" src="http://www.8box.cn/javascripts/prototype.js"></script>
    </body>
    </html>
      

  3.   

    有难度,没看过用div做的好的editor,lz做好了到csdn分享下,
    先看楼下怎么说:
      

  4.   

    在光标处插入图片,如果焦点仍然在iframe里的话,可以直接插入的,参见:Rich-Text Editing in Mozillahttp://www.easyui.org.cn/easyRT_demo.html
      

  5.   

    <script language="javascript"> 
    //在光标处插入指定内容
    //A:要插入内容 T:插入控件
    var _insertText = function(A,T){ 
    var B = document.getElementById(T); 
    B.focus(); 
    var C = document.selection.createRange(); 
    if(C){ 
        if(C.text.length > 0) C.text += A;else C.text= A; 
        C.select(); 


    </script>
    <div id="sProductName" NAME="sProductName" contentEditable=true style="width:400px;height:300px;border:1px solid #999999;"></div> <input type="button" value="插入换行符" onclick="_insertText('<br />','sProductName')">
    今天在51上看到的,略改了下,希望对LZ有用