Uncaught IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index

前端 未结 4 1826
無奈伤痛
無奈伤痛 2020-12-03 04:54

What is problem here?

if ( window.getSelection() ) 
  EditField = window.getSelection().getRangeAt(0);

throws error:

<
4条回答
  •  清歌不尽
    2020-12-03 05:31

    Here is something to consider: have you disabled selection somewhere in your code? If so, in some situations you may want to first enable selection, preferably in a mousedown event or before focusing on your element (NB!), before getting the range. Doing this has solved my version of the problem.

    $('#MyEditableDiv').on('mousedown', function (e) {
    
        $(document).enableSelection();
    
    
    });
    

提交回复
热议问题