我用到了一个iframe
希望在给他添加一个onkeydown事件,以便给一个hidden赋值
代码如下<iframe id="message" marginwidth="0" marginheight="0" width="100%" height="250px"
                style="border-style: solid; border-width: 1px; border-color: Gray; border-bottom-width: 0px;"
                frameborder="0"></iframe>
<input name="HtmlEditor1" type="hidden" id="HtmlEditor1"
    runat="server" /><script language="javascript" type="text/javascript">
    function initframe()
    {
        message.document.designMode="On";   
        message.document.body.onkeydown=function setContent()
        {
            document.getElementById("HtmlEditor1").value = message.document.body.innerHTML ;
        }
     }
    window.onload=initframe;
</script>却无法给该HtmlEditor1赋值,望指出错误,给出正解

解决方案 »

  1.   

    不会。是不是可以再iframe外面套一个div之类的,使用div的onkeydown呢?
      

  2.   

    document.getElementById("<%=HtmlEditor1.ClientID%>").value = document.getElementById('message').document.body.innerHTML
      

  3.   

    回8楼,我希望他可以自动把我输入的内容存到HtmlEditor1中
    因为这是在一个用户控件中
    我在后台这样写的 public string Content
        {
            get { return HtmlEditor1.Value.ToString(); }
            set { HtmlEditor1.Value = value; }
        }然后我可以再引用该控件的页面来获取该iframe的内容
    当然可以给出好的解决方法
      

  4.   


    <script language="javascript" type="text/javascript">
            function initframe() {
                debugger
            document.getElementById('message').document.designMode="On";
            document.getElementById('message').document.body.onkeydown = function setContent() {
            document.getElementById('HtmlEditor1').value = document.getElementById('message').document.body.innerHTML;
            }
         }
        window.onload=initframe;
    </script>
      

  5.   

    12楼方法已经测试
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            function initframe() {
                debugger
            document.getElementById('message').document.designMode="On";
            document.getElementById('message').document.body.onkeydown = function setContent() {
            document.getElementById('HtmlEditor1').value = document.getElementById('message').document.body.innerHTML;
            }
         }
        window.onload=initframe;
    </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <iframe id="message" marginwidth="0" marginheight="0" width="100%" height="250px"
                    style="border-style: solid; border-width: 1px; border-color: Gray; border-bottom-width: 0px;"
                    frameborder="0"></iframe> 
            <input name="HtmlEditor1" type="hidden" id="HtmlEditor1"  runat="server"/>
            <input name="HtmlEditor2" type="button" id="HtmlEditor2" value="GetValue" onclick="alert(document.getElementById('HtmlEditor1').value)"/>    </div>
        </form>
    </body>
    </html>
    点击GetValue 试试看
      

  6.   

    <script language="javascript" type="text/javascript">
            function initframe() {
                debugger
                message.document.designMode = "On";
                message.document.onkeyup = function setContent() {
                document.getElementById('HtmlEditor1').value = message.document.body.innerHTML;
                }
            }
            window.onload=initframe;
        </script>
    这个可能才是你想要的
      

  7.   

    回 14L
    浏览器IE8
    赋值你的代码,可是我无法选中iframe ,即我不能输入值
    故也不能测试,为什么??
      

  8.   

    理解错误 我用的也是IE8  用#15的代码替换#14的js   你是要做一个图文编辑器吧
      

  9.   

    jquery的几款editor还不错 可以google一下 jquery editor
      

  10.   

    恩,这个Lightweight Rich Text Editor还可以,小巧
    谢谢了
    只是可惜了我这个问题没有解决啊
    无奈。
      

  11.   

    额,谢谢xxyxxyxxyxxyxxyou兄弟一直热心回答
    这个问题我在JavaScript版搞定了
    谢谢了