問題描述
我有一個顯示一些跨度的 React 應用程序:
I have a React application which displays some spans:
<span>Hello</span> <span>my</span> <span>name</span> <span> is </span> ...
我希望用戶像這樣用鼠標選擇文本
I would like the user to select the text with the mouse like so
..然后獲取選定的值,或突出顯示文本等.我將如何在 React 中做到這一點?我不確定要使用哪些事件處理程序以及如何獲取當前選擇!一個最小的例子或提示將不勝感激!
..and then get the selected value, or highlight the text etc. How would I do this in React? I am not sure what event handlers to use and how to get hold of the current selection! A minimal example or a hint would be appreciated!
推薦答案
沒有 React 特定的解決方案.只需使用 window.getSelection API.
There is no React-specific solution for this. Just use window.getSelection API.
要輸出突出顯示的文本,請運行 window.getSelection().toString()
To output highlighted text run window.getSelection().toString()
這篇關于選擇文本和突出顯示選擇或獲取選擇值(React)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!