selectionStart and selectionEnd in contenteditable element

后端 未结 2 2024
天命终不由人
天命终不由人 2020-12-29 21:41

I have been struggling the selectionStart and selectionEnd attributes of textarea to make them work with contenteditable d

2条回答
  •  孤独总比滥情好
    2020-12-29 22:14

    This answer uses Selection#modify, which is non-standard, but at least, I suggest you to use "insertText" command:

    function replaceVal(val, step) {
      var selection = window.getSelection();
      for (var i = 0; i < step; i += 1) {
        selection.modify('extend', 'backward', 'character');
      }
      document.execCommand('insertText', false, val);
    }
    
    
    Test test test

提交回复
热议问题