简体   繁体   English

如何将 css 应用于 textarea 标签内用逗号分隔的文本? (React.js 项目)

[英]How do I apply css to text separated by commas inside of a textarea tag? (React.js Project)

Desired result:期望的结果:

期望的结果


I'm trying to highlight text separated by commas inside of a textarea tag (in a React project).我正在尝试突出显示 textarea 标记内用逗号分隔的文本(在 React 项目中)。

I really can't figure out how to accomplish this, yet every solution I've come across online uses jQuery (I really don't want to inject jQuery halfway through my project).我真的不知道如何做到这一点,但我在网上遇到的每个解决方案都使用 jQuery(我真的不想在我的项目中途注入 jQuery)。

This is the function I currently have to trim the input values and split them:这是 function 我目前必须修剪输入值并将它们拆分:

handleTags = async (event) => {
    let tags = event.target.value.toLowerCase().trim().replace(/[^\w,-]/g, '')
    tags = tags.split(",")
    await this.setState({post: {...this.state.post, tags: tags}})
}

And this is the JSX for the textarea within my render() {...} function:这是我的render() {...} function 中 textarea 的 JSX:

<Card.Footer className="quickpost-tags-container">
 <textarea
   onChange={event => this.handleTags(event)}
   className="postbox-tags-textarea"
   placeholder="science, non-fiction, etc..."
   contentEditable
   suppressContentEditableWarning>
 </textarea>
</Card.Footer>

I'd appreciate any help.我会很感激任何帮助。

So I understand that this question was a bit meh, didn't make much sense and made me sound like a massive noob (it was like 4 in the morning).所以我知道这个问题有点意思,没有多大意义让我听起来像个大菜鸟(就像早上 4 点一样)。 I was trying to get the tags that the user would put in to render the same as [ refer to the image in the question ]我试图让用户放入的标签呈现与[参考问题中的图像]相同

I was able to conditionally render a span element above the textarea which would render each tag within a span element, dynamically.我能够有条件地在 textarea 上方渲染一个span元素,这将动态地渲染span元素中的每个标签。 This achieved the desired result.这达到了预期的结果。

在此处输入图像描述

Hopefully, this will be helpful to someone looking to solve a similar problem.希望这对希望解决类似问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM