How can I style text input as I type?

前端 未结 3 1543
予麋鹿
予麋鹿 2021-01-12 14:15

Every time I type a character in an input field, I parse the expression so that I get:

  1. An array of tokens, i.e., tokenArray=[2, *, COS, (, 4, )]
  2. A cor
相关标签:
3条回答
  • 2021-01-12 14:24

    as Matt said, <input>s don't support styling content. However, one possible solution could be to use a second <span> that will contain the input's value, and show the styled content here. If that's not acceptable, then use contenteditable as Matt suggested.

    0 讨论(0)
  • 2021-01-12 14:39

    Text inputs do not support styled content. End of story.

    A common solution is to use contenteditable instead.

    0 讨论(0)
  • 2021-01-12 14:42

    You can sort of hack styling behind it if it's just for unimportant aesthetics.

    • Make the background and the text of the <input> transparent
    • Have another <span> behind it
    • Update the content of the <span> (with appropriate styling) when the <input>'s contents change

    Here's a quick demo.

    0 讨论(0)
提交回复
热议问题