execCommand(“insertHTML”, …) in Internet Explorer

后端 未结 4 1674
旧时难觅i
旧时难觅i 2020-12-06 06:23

I\'m building a wysiwyg-editor with an editable iframe using document.execCommand(). Now i need to use the \"insertHTML\" command which works perfe

4条回答
  •  佛祖请我去吃肉
    2020-12-06 06:56

    In IE <= 10 you can use the pasteHTML method of the TextRange representing the selection:

    var doc = document.getElementById("your_iframe").contentWindow.document;
    
    if (doc.selection && doc.selection.createRange) {
        var range = doc.selection.createRange();
        if (range.pasteHTML) {
            range.pasteHTML("Some bold text");
        }
    }
    

    UPDATE

    In IE 11, document.selection is gone and insertHTML is still not supported, so you'll need something like the following:

    https://stackoverflow.com/a/6691294/96100

提交回复
热议问题