iframe 中的 document.execCommand 不起作用

发布于 2024-11-16 17:24:35 字数 1213 浏览 2 评论 0原文

我正在开发我的所见即所得编辑器。我有这样的代码:

doc.execCommand(cmd, false, null);

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

doc = iframe1.contentWindow.document; 

它在 Chrome 中工作正常,但在 IE(我的是 IE9)中根本不起作用。

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

我已经通过互联网进行搜索,但找不到可用的解决方案。

有人愿意帮助我吗?

代码示例:

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); 
};

I'm working on my WYSIWYG editor. And I have code like this:

doc.execCommand(cmd, false, null);

The cmd argument would be 'Bold', 'Italic', etc. And the doc variable refer to the document in the iframe, which is initialized somewhere else:

doc = iframe1.contentWindow.document; 

It works fine in Chrome, but in IE(my is IE9) doesn't work at all.

I debugged my code with the developer tool and found nothing wrong, the execCommand function just doesn't work.

I have searched through the Internet and couldn't found an available solution.

Would anyone give me a help?

Code sample:

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); 
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2024-11-23 17:24:35

这是因为在不同的浏览器中使用 iframe 有不同的方法

,这是它应该如何工作

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

更新

好吧,我想我犯了一个小错误,它应该是这样的:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文