在调用插入之前,先让iframe获得焦点,iframename.focus()

解决方案 »

  1.   

    和焦点没什么关系,你的方法不对。用下面这个。
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>新建网页 1</title>
    </head><body><iframe src="" frameborder="1" style="width:650px; height:500px" id="fraEdit" name="fraEdit"></iframe>
    JS文件:
    //插入文本框
    <script>
    function insertInputText() {
    try{
      fraEdit.document.body.innerHTML=fraEdit.document.body.innerHTML+"<input>"
      } catch(err) {
       }
    }
    </script>
    我的父页面有一个下拉框:
    <select name="select" onChange="insertInputText();">
      <option value="title" selected>标题</option>
      <option value="content">正文</option>
    </select></body></html>
      

  2.   

    <!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">
    <!-- DW6 -->
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>shawl.qiu template</title>
    <script type="text/javascript">
    //<![CDATA[
    onload=function(){
    var o=document.getElementById('ifm').contentWindow
    o.document.designMode='on';
    o.contentEditable='on';
    o=null;
    }
    function test(obj){ 
    if(!obj)return false;
    obj.focus()
    var slct=obj.document.selection;
    var rg=slct.createRange(); 
    rg.pasteHTML('<h2>test this</h2>'); // IE Only
    obj=slct=rg=null;
    }
    //]]>
    </script>
    </head>
    <body>
    <iframe src="about:blank" width="500" height="200" id="ifm"></iframe><br />
    <button onclick="test(document.getElementById('ifm').contentWindow)">test</button>
    </body>
    </html>
      

  3.   

    to myvicy(我来也!) 
    我试了你的方法,你的不是在光标停留的位置上添加,是追加在最后面了
      

  4.   

    光标我原有的方法就可以获取了,现在就是想办法禁止往父页面里写入就行了,我奇怪,父页面的designMode我没设定阿,btbtd的方法倒是可以,能不能来讲讲原理
      

  5.   

    用range判断当前range是否在iframe的document上。如果不是,则把焦点停在iframe上