iframe 中的 document.execCommand 不起作用
我正在开发我的所见即所得编辑器。我有这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为在不同的浏览器中使用 iframe 有不同的方法
,这是它应该如何工作
更新
好吧,我想我犯了一个小错误,它应该是这样的:
That is because there are different methods to work with iframe in different browsers
here is how it should work
UPDATE
ok i think i made a little mistake here is how it should look like: