Get bounding rectangle of selected text javascript

后端 未结 4 1195
情深已故
情深已故 2021-02-14 21:59

I use this code How can I position an element next to user text selection? to get the position of the selected text, but it doesn\'t work for the selected text inside an input.

4条回答
  •  执笔经年
    2021-02-14 22:36

    You can use the following code to get the position of selected text:

    var selection = window.getSelection();
    var getRange = selection.getRangeAt(0); 
    getRect = getRange.getBoundingClientRect();
    

提交回复
热议问题