简体   繁体   English

使用受控输入响应 contentEditable div

[英]React contentEditable div with controlled input

Is there any way to have a controlled input contentEditable div?有什么办法可以控制输入 contentEditable div?

I am looking for this exact behavior, but I need to use a contentEditable div instead of an input:我正在寻找这种确切的行为,但我需要使用 contentEditable div 而不是输入:

<input value={this.state.text}></input>

where the input doesn't show what is typed, but rather what is in this.state.text .输入不显示输入的内容,而是显示this.state.text

Just swapping out with a contentEditable div doesn't work, and it shows what is typed rather than what is in this.state.text :只是换出一个 contentEditable div 是行不通的,它显示了输入的内容而不是this.state.text

<div contentEditable={true} value={this.state.text}></div>

Stackblitz: https://stackblitz.com/edit/react-q4xoya Stackblitz: https ://stackblitz.com/edit/react-q4xoya

Value is not a valid attribute for a div . Value 不是div的有效属性。 It can be done, but is not officially supported and is certainly unorthodox.它可以做到,但没有官方支持,而且肯定是非正统的。

I suggest you change this line from your stackblitz:我建议您从 stackblitz 更改此行:

<div style={{border: '1px solid black'}} contentEditable={true} value={this.state.text}></div>

to this:对此:

<div style={{border: '1px solid black'}} contentEditable={true}>{this.state.text}</div>

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

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