execCommand(“insertHTML”, …) in Internet Explorer

后端 未结 4 1673
旧时难觅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:59

    var doc = document.getElementById("your_iframe").contentWindow.document;
    
    // IE <= 10
    if (document.selection){
        var range = doc.selection.createRange();
            range.pasteHTML("Some bold text");
    
    // IE 11 && Firefox, Opera .....
    }else if(document.getSelection){
        var range = doc.getSelection().getRangeAt(0);
        var nnode = doc.createElement("b");
            range.surroundContents(nnode);
            nnode.innerHTML = "Some bold text";
    };
    

提交回复
热议问题