123456789123456789
求代码。如在: 
123456789 把焦点放在5 然后按 csdn 插入图像弹出的URL地址。确定后,代码自动会在5前面。然不是在字符最后12345[ i m g = h ttp://]xxx[/ i mg]6789           yes   我要这种效果123456789[ i m g = h ttp://]xxx[/ i mg]        no 

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>测试</title>
    </head>
    <body>
    <div id=a>123456789123456789</div><br>
    选中要添加链接的数字,然后点击按钮
    <div><input type="button" value="加链接" onclick="javascript:LinkIt();" /></div>
    </body>
    </html>
    <script type="text/javascript" language="javascript">
    function LinkIt(){
      var o=document.getElementById("a");
      var r=document.selection.createRange();
      var s=r.text;
      s +="";
      document.selection.empty();
      r.text = s;
    }
    </script>
      

  2.   

    核心就是一个document.selection.createRange()
    前提是需要“选中”那个数字,而不是光标移动到数字后面。这个和楼主的需求有点差异,呵呵
      

  3.   


    <script>
    function doit(){
    document.getElementById("editor").contentWindow.focus();
    sel = document.getElementById("editor").contentWindow.document.selection.createRange();
    sel.pasteHTML("");
    }
    window.onload = function(){
    document.getElementById("editor").contentWindow.document.body.contentEditable = true ;
    document.getElementById("editor").contentWindow.document.body.innerHTML = "123456";

    }
    </script>
    <iframe id="editor" name="editor" src="about:blank"></iframe>
    <input type="button" onclick="doit()" value="插入">