Mar*_*tin 8 javascript internet-explorer internet-explorer-8
我正在使用可编辑的iframe构建一个所见即所得的编辑器document.execCommand().现在我需要使用"insertHTML"在Chrome和Firefox中完美运行的命令,但当然它在Internet Explorer中不起作用:
function run() {
document.getElementById("target").focus();
document.execCommand("insertHTML", false, "<b>ins</b>");
}Run Code Online (Sandbox Code Playgroud)
<div contenteditable id="target">contenteditable</div>
<button onclick="run()">contenteditable.focus() + document.execCommand("insertHTML", false, "<b>ins</b>")</button>Run Code Online (Sandbox Code Playgroud)
这个问题的标准解决方案是什么?没关系,如果它只适用于IE8,但IE7支持也会很好.
Tim*_*own 15
在IE <= 10中,您可以使用表示选择的pasteHTML方法TextRange:
var doc = document.getElementById("your_iframe").contentWindow.document;
if (doc.selection && doc.selection.createRange) {
var range = doc.selection.createRange();
if (range.pasteHTML) {
range.pasteHTML("<b>Some bold text</b>");
}
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
在IE 11中,document.selection已经消失,insertHTML仍然不受支持,因此您需要以下内容:
| 归档时间: |
|
| 查看次数: |
23766 次 |
| 最近记录: |