3

我正在开发我的 WYSIWYG 编辑器。我有这样的代码:

doc.execCommand(cmd, false, null);

cmd参数将是“Bold”、“Italic”等。doc变量引用 iframe 中的文档,该文档在其他地方初始化:

doc = iframe1.contentWindow.document; 

它在 Chrome 中运行良好,但在 IE(我的是 IE9)中根本不起作用。

我用开发工具调试了我的代码,没有发现任何问题,execCommand函数不起作用。

我已经通过 Internet 搜索并找不到可用的解决方案。

有人会给我帮助吗?

代码示例:

function $see(e, o) {
    var that = this;
    ...
    this.e = $see.make('iframe', { 'class': 'editor' }); // editor iframe    
    this.e.onload = function () {  // call when the document is loaded
        var d = that.e.contentWindow || that.e.contentDocument;
        if (d.document) d = d.document;
        that.doc = d;
        that.doc.write('<html><head></head><body></body></html>');
        that.doc.body.innerHTML = that.ta.value; // that.ta refers to an textarea
        that.doc.body.setAttribute('contenteditable', 'true');
        ...
    };
}
$see.prototype.exec = function (cmd) { 
    // call in an <a> tag's onclick event outside the iframe
    this.doc.execCommand(cmd, false, null); 
};
4

1 回答 1

3

That is because there are different methods to work with iframe in different browsers

here is how it should work

var doc= iframe1.contentWindow || iframe1.contentDocument;
if (doc.document)
 doc=doc.document;

UPDATE

ok i think i made a little mistake here is how it should look like:

var doc = iframe.contentWindow || iframe.contentDocument.defaultView;
if (doc.document)
doc=doc.document;
于 2011-06-24T05:42:25.897 回答