javascript中的实时文本颜色更改 [英] Live text color change in javascript

查看:111
本文介绍了javascript中的实时文本颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我正在做一个小项目。我有一个用户输入单词的textarea。当用户输入时,如果他们使用某些单词,我需要文本来改变颜色。例如,当露西正在输入句子棕色的母牛跳过月亮时,文字会使棕色这个词变为棕色,但是在她将褐色之后的空间键入后,就会生活。我希望你理解:P哦和jquery是一个选项。

I'm working on a small project right now. I have a textarea that the user types words into. While the user is typing, I need the text to change color if they use certain words. For example, as lucy is typing the sentence "The brown cow jumped over the moon" the text would make the word "brown" a brown color, but live right after she types the space after "brown". I hope you understand :P Oh and jquery is an option.

推荐答案

如前所述,它不可能实现在常规文本区域中对表示的必要控制水平。

As has been said before, it's not possible to achieve the required level of control over presentation in a regular textarea.

以下是关于该问题的几个stackoverflow问题:

Here are a couple of stackoverflow questions on the issue:

  • Textarea that can do syntax highlighting on the fly?
  • https://stackoverflow.com/questions/1505761/textarea-with-syntax-code-highlighting

你可以看看使用 codepress 之类的内容并编辑源代码以突出显示您想要的单词。

You could look at using something like codepress and editing the source code to highlight the words you want.

或者,如果您想从头开始,可以使用< div contentEdita ble =true>< / div>而不是这里建议的textarea:

使用jQuery在textarea中着色文本

Or if you wanted to do it from scratch you could use <div contentEditable="true"></div> instead of a textarea as suggested here:
colorize text in textarea with jQuery

以下是一些没有jquery span替换的示例代码:

http://jsfiddle.net/s5uaJ/

Here is some example code without the jquery span replacement:
http://jsfiddle.net/s5uaJ/

祝你好运

这篇关于javascript中的实时文本颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆