Set cursor position on contentEditable

后端 未结 8 2168
悲&欢浪女
悲&欢浪女 2020-11-22 09:05

I am after a definitive, cross-browser solution to set the cursor/caret position to the last known position when a contentEditable=\'on\'

regains focus. It appears
8条回答
  •  借酒劲吻你
    2020-11-22 09:33

    You can leverage selectNodeContents which is supported by modern browsers.

    var el = document.getElementById('idOfYoursContentEditable');
    var selection = window.getSelection();
    var range = document.createRange();
    selection.removeAllRanges();
    range.selectNodeContents(el);
    range.collapse(false);
    selection.addRange(range);
    el.focus();
    

提交回复
热议问题