在页面里套用了一个iframe,然后动态给iframe添加了一个div,给设置为可编辑状态,怎么能让程序运行的时候鼠标的焦点就在这个div中?还有一个问题是如果我运行页面,然后全选删除,那么那个div也跟着删掉了 ,有什么方法可以保留这个div么?

解决方案 »

  1.   

    动态添加的div的id为div1:
    document.getElementById("iframe1").contentWindow.document.getElementById("div1").focus();
      

  2.   


    我这个div 是用Js加进去的,这么写总是报脚本错误 
    document.getElementById(...).contentWindow.document.write(.' 为空或不是对象
      

  3.   


         <script type="text/javascript">
            window.onload = function () {
                document.getElementById("showValue").contentWindow.document.write("<html><body style='margin:0px'><div id='showMSG' style='color:<%=color %>;font-size:<%=size %>px;font-family:<%=font %>;word-spacing:5px;letter-spacing:2px;padding:0px;margin:0px; line-height:20px;z-index:100;background:#fff;left:0;top:0;work-wrap:break-word;overflow:hidden' scrolling='no' onload='this.height=showValue.document.body.scrollHeight+20' > </div></body></html>");
                document.getElementById("showValue").contentWindow.document.getElementById("showMSG").focus();
                document.getElementById("showValue").contentWindow.document.designMode = "on";
                document.getElementById("showValue").contentWindow.document.contentEditable = true;
               
            }
        </script>    这个是我的js代码,设置focus了可是还是不好用有哪位知道是什么原因?
      

  4.   

    这样:
        <script type="text/javascript">
            window.onload = function() {
                document.getElementById("showValue").contentWindow.document.write("<html><body style='margin:0px'><div id='showMSG'  style='border:solid 1px #ccc;' ></div></body></html>");
                //document.getElementById("showValue").contentWindow.document.getElementById("showMSG").innerHTML="aaaa";
                document.getElementById("showValue").contentWindow.document.getElementById("showMSG").contentEditable = "true";
                document.getElementById("showValue").contentWindow.document.getElementById("showMSG").focus();
                //document.getElementById("showValue").contentWindow.document.designMode = "on";
            }
        </script>
      

  5.   

    第二个问题:将div取出,删除后再添加一遍!
      

  6.   

    不可编辑对象 focus 不起作用  先 contentEditable  后 focus
      

  7.   


    我再最后设置了focus也是不好用啊初始的时候还是不能捕获鼠标,我现在把代码进行了调整,第二个问题解决了,代码如下:
     <script type="text/javascript">
            window.onload = function () {
                document.getElementById("showValue").contentWindow.document.write("<html><body style='margin:0px;color:<%=color %>;font-size:<%=size %>px;font-family:<%=font %>;word-spacing:5px;letter-spacing:2px;padding:0px;margin:0px; line-height:20px;z-index:100;background:#fff;left:0;top:0;work-wrap:break-word;overflow:hidden' scrolling='no' onload='this.height=showValue.document.body.scrollHeight+20'></body></html>");
                document.getElementById("showValue").contentWindow.document.designMode = "on";
                document.getElementById("showValue").contentWindow.document.contentEditable = true;
                document.getElementById("showValue").contentWindow.focus();
            }
        </script>