Insert html at caret in a contenteditable div

后端 未结 4 1454
予麋鹿
予麋鹿 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:29

    var r = getSelection().getRangeAt(0);
    r.insertNode(r.createContextualFragment('Hello'));
    
    //select this range
    getSelection().removeAllRanges();
    getSelection().addRange(r);
    //collapse to end/start 
    getSelection().collapseToEnd() 
    

提交回复
热议问题