【问题标题】:Why a pop-up alert can affect "designMode"?为什么弹出警报会影响“designMode”?
【发布时间】:2009-04-12 06:07:03
【问题描述】:

我正在尝试构建一个页面编辑器。一个问题让我在 Firefox 中发疯。

页面代码如下:

<body>
<iframe WIDTH=200 HEIGHT=200 id="myEditor"></iframe>
<script>

    function getIFrameDocument(sID){
        // if contentDocument exists, W3C compliant (Mozilla)
        if (document.getElementById(sID).contentDocument){
            alert("mozilla"); // comment out this line and it doesn't work
            return document.getElementById(sID).contentDocument;
        } else {
            // IE
            alert("IE");
            //return document.getElementById(sID);
            return document.frames[sID].document;
        }
    }

    getIFrameDocument("myEditor").designMode = "On";

</script>

</body>

它只是检查是否适合以Mozilla方式或IE方式设置“designMode”。当页面加载时,会弹出一个“Mozilla”;点击 iframe 区域,焦点在 iframe 上,我可以用键盘输入。

这看起来不错,但是当我注释掉 “alert("mozilla");” 行时,它不起作用。如 FireBug 所示,“designMode”为“Off”。

这太有线了。为什么警报会影响 DOM 和 javascript? 顺便说一句,我的 Firefox 是 3.0.6。

【问题讨论】:

    标签: javascript firefox alert designmode


    【解决方案1】:

    因为警报为 iframe 提供了加载时间。只有在 iframe 文档加载后,您才应将 designMode 设置为“on”:

    iframe.onload = function() {
        doc.designMode = "on";
    };
    

    【讨论】:

    • 谢谢。修复使它工作! “加载 iframe 后可以开启 designMode”只适用于 Firefox,对吗?看来IE没有这个限制。
    • 我不太记得这件事了。上次我玩设计模式时,我在两个浏览器中都应用了“onload”,但是,是的,我第一次在 FF 中观察到它。无论如何,我确实记得在 IE 中动态生成的 iframe 存在问题。所以也要小心那些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 2019-08-17
    • 2017-11-01
    • 2016-12-09
    • 2022-11-06
    相关资源
    最近更新 更多