简体   繁体   English

如何将字符串添加到具有不同颜色的文本区域?

[英]How to add a string to a text area with different colours?

I am trying to work out how to write a function where I pass in a string and this string is added to a textarea. 我试图找出如何写一个函数,我传入一个字符串,这个字符串被添加到textarea。 This I can do, but I want to parse that string and change the colour on different parts of the string. 我可以这样做,但我想解析该字符串并更改字符串不同部分的颜色。

In other words, find the words "select", "where" and turn them blue. 换句话说,找到单词“select”,“where”并将它们变为蓝色。 Then find the words "AND", "OR", "<" and turn them grey and anything in between two single quotes to be the colour red. 然后找到单词“AND”,“OR”,“<”并将它们变为灰色,两个单引号之间的任何内容都为红色。

I am hoping for a simple function rather than using syntax highlighting libraries, I am not even sure they will work with strings that are dynamically generated. 我希望有一个简单的函数,而不是使用语法高亮库,我甚至不确定它们是否可以使用动态生成的字符串。

How can I do this? 我怎样才能做到这一点? I am able to make use of JQuery if this makes things easier? 如果这样可以让事情变得更容易,我可以使用JQuery吗?

Thanks all 谢谢大家

text inside <textarea> elements is plain text only, meaning that it cannot be styled. <textarea>元素内的文本只是纯文本,这意味着它无法设置样式。 what you can do is create a div with contenteditable and work with that, check this link out for a reference: http://www.west-wind.com/Weblog/posts/778165.aspx 你可以做的是创建一个具有contenteditable的div并使用它,请查看此链接以供参考: http//www.west-wind.com/Weblog/posts/778165.aspx

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

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