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

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

I have a React application which displays some spans:

Hello my name  is &l         


        
6条回答
  •  死守一世寂寞
    2021-02-02 02:54

    Here's an example in React using a functional component:

    const Post = () => {
        const handleMouseUp() {
            console.log(`Selected text: ${window.getSelection().toString()}`);
        }
        return (
            
    Text
    ); }

    As Lyubomir pointed out the window.getSelection() API does the trick!

提交回复
热议问题