Select Text & highlight selection or get selection value (React)

后端 未结 6 1296
长发绾君心
长发绾君心 2021-02-02 02:33

I have a React application which displays some spans:

Hello my name  is &l         


        
6条回答
  •  忘了有多久
    2021-02-02 02:52

    Use onMouseUp and onDoubleClick events to detect to call method, that will determine selection using JavaScript Selection API.

    Use window.getSelection() to get selection object.

    To get selected text, use window.getSelection.toString().

    To get coordinates of selected text area for rendering popup menu, use selection.getRangeAt(0).getBoundingClientRect().

    As an example implementation, take a look at react-highlight library.

提交回复
热议问题