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

后端 未结 6 1238
长发绾君心
长发绾君心 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

    Due to this bug in Firefox, I was unable to get it working with window.getSelection() as suggested by other answers.

    So I had to do the following (in React):

    constructor(props) {
        this.textAreaRef = React.createRef();
    }
    
    getSelection() {
        const textArea = (this.textAreaRef.current as HTMLTextAreaElement);
        console.log(textArea.value.substring(
                textArea.selectionStart,
                textArea.selectionEnd
            )
        );
    }
    
    render() {
        
    }
    

提交回复
热议问题