如何在文本中添加一些特定单词的颜色react js?
我正在开发一个包含一些特定单词的文本区域(例如,它们可以以“%”:%qwerty%开始/结束)。我希望这些单词具有不同的颜色(例如:绿色),并且文本区域仍然可以编辑。 我尝试过一些解决方案,但它们无法正常工作,这就是为什么我想询问社区如何实施文本输入区域。
我的代码在这里 https://codesandbox.io/codesandbox.io/s/festive-cache -sshmjd?file =/src/tagsinput.js
组合成一个单个,而不是将单个组合成一个单个单词
在第1和第二个输入中,我对单词进行了分类和使用的映射()以在标签中显示它们,但是,它们变成了分离的输入,而不是在一个中 第三输入,我为特殊单词实现了一个,但这是一个固定的字符串,我不知道如何像状态变量一样实现它来检测输入区域中的任何更改。
有人可以查看我的代码并提供帮助吗?
I am developing a text area that contains some specific words(Eg. they can start/end with "%": %QWERTY%). I want these words to have a different color(E.g: Green) and the text area is still editable.
I have tried some solutions but they do not work as I expected, that is why I would like to ask the community how to implement a text input area.
My code is here
https://codesandbox.io/s/festive-cache-sshmjd?file=/src/TagsInput.js
In the 1st and 2nd inputs, I classified words and used map() to display them in tags, however, they became separated inputs, not combine into a single one
In the 3rd input, I implemented a for the special word, but this is a fixed string, and I do not know how to implement it like a state variable to detect any changes in the input area.
Could somebody review my code and help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有效,一旦输入不再集中,它就会为目标词染色。
它可以通过将文本保存到状态Onblur中,每当看到目标单词“ red”时将其分开。
This works, it colors the target word once the input is no longer focused.
It works by saving the text into state onBlur, separating the string whenever it sees the target word "red".