Insert html at caret in a contenteditable div

后端 未结 4 1453
予麋鹿
予麋鹿 2020-11-22 06:57

I have a div with contenteditable set and I am capturing keypress using jquery to call preventDefault() when the enter key is pressed. Similar to this question which insert

4条回答
  •  旧巷少年郎
    2020-11-22 07:36

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

提交回复
热议问题