开发者

Textarea - how to highlight keystrings with javascript?

开发者 https://www.devze.com 2023-02-21 20:15 出处:网络
I am looking for a javascript that can help me to change the color of a text inside the textarea tag. For example, to have a variable in the javascript:

I am looking for a javascript that can help me to change the color of a text inside the textarea tag. For example, to have a variable in the javascript:

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

now, the javascript should make the text that开发者_StackOverflow中文版 is inside the variable, to be displayed as bold with red color in the textarea.


See this previous question/answer: jQuery wrap selected text in a textarea


Inner HTML of the textarea element you cannot change the styles/colors of the partial words or characters.

You should use or some other element to implement this.

You can consider the

contenteditable="true" attribute for this purpose.

By using this attribute you can dynamically edit any html element. 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. I think a better option would be to use a library like CodeMirror or MDK-Editor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号