javascript replace selection all browsers

前端 未结 2 765
甜味超标
甜味超标 2020-12-14 10:35

Is there a simple js function I can use to replace the current document\'s selection with some html of mine?

For instance say the document contains a

2条回答
  •  太阳男子
    2020-12-14 11:26

    You can use the Rangy library http://code.google.com/p/rangy/

    You can then do

    var sel = rangy.getSelection();
    var range = sel.getRangeAt(0);
    
    range.deleteContents();
    var node = range.createContextualFragment('hoho');
    range.insertNode(node);
    

提交回复
热议问题