Inserting caret after an inserted node

后端 未结 2 1767
南方客
南方客 2021-02-04 10:20

So I have a method that takes a tag and wraps the selected text in that tag.

function wrap(tag) 
{               
    var sel, range;
    if (window.getSelectio         


        
相关标签:
2条回答
  • 2021-02-04 10:46

    do this after inserting the node to the range range.collapse(false); this will change position of selection range to the end of the range, so my guess is it should set the cursor at end position

    0 讨论(0)
  • 2021-02-04 10:57

    You can use the range.setStartAfter and range.setEndAfter methods to set the start and end points to the point directly after your new node. I setup a jsfiddle example here: http://jsfiddle.net/phil_mcc/tM3mA/

    //move the caret
    range.setStartAfter(newNode);
    range.setEndAfter(newNode); 
    sel.removeAllRanges();
    sel.addRange(range);
    
    0 讨论(0)
提交回复
热议问题