0

I've seen folks alternate colors of characters inside a single element by dynamically wrapping each in a span and targeting them individually, but I want to alternate the color of each character in an input field ( say for example alternate between red, green, blue ) ...is possible? I'm stumped...

4

1 回答 1

1

可以使用 DIV 来模拟它,其中 contentEditable 属性设置为 true 并设置为输入框。

在最基本的情况下,当您输入它时,您可以处理诸如“onkeydown”之类的事件,并在将字符添加到 DIV 之前使用给定颜色的 SPAN 包围字符。

这样,您可以在键入时在“输入”字段中实时为字符着色。

如果您必须处理粘贴/删除,它会变得更加复杂,但这就是想法。

于 2013-09-03T04:05:10.320 回答