简体   繁体   English

Textarea-如何使用JavaScript突出显示键串?

[英]Textarea - how to highlight keystrings with javascript?

I am looking for a javascript that can help me to change the color of a text inside the textarea tag. 我正在寻找可以帮助我更改textarea标签内文本颜色的JavaScript。 For example, to have a variable in the javascript: 例如,要在javascript中有一个变量:

var a = '<div class="carleft"><p class="coto1">';

now, the javascript should make the text that is inside the variable, to be displayed as bold with red color in the textarea. 现在,javascript应该使变量内的文本在文本区域中以红色显示为粗体。

参见前面的问题/答案: jQuery将选定的文本包装在textarea中

Inner HTML of the textarea element you cannot change the styles/colors of the partial words or characters. textarea元素的内部HTML不能更改部分单词或字符的样式/颜色。

You should use or some other element to implement this. 您应该使用或其他一些元素来实现这一点。

You can consider the 您可以考虑

contenteditable="true" attribute for this purpose. contenteditable =“ true”属性用于此目的。

By using this attribute you can dynamically edit any html element. 通过使用此属性,您可以动态编辑任何html元素。 Which was styled before. 以前是样式的。

A textarea does not support different styles or colors in the text. 文本区域不支持文本中的不同样式或颜色。 You can use contenteditable="true" , but that will probably give the user more freedom than you want. 您可以使用contenteditable="true" ,但这可能会给用户带来比您想要的更多的自由。 I think a better option would be to use a library like CodeMirror or MDK-Editor . 我认为更好的选择是使用像CodeMirrorMDK-Editor这样的库。

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

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