Soy un novato en reaccionar. Tengo un div editable de contenido. Lo que quiero es hacer un botón, al hacer clic en el botón -> se agregará la palabra "HEY". Digamos que la longitud de mi contenido es 100, y he hecho clic en los 5 caracteres iniciales y he dado el espacio y he hecho clic en el botón Pegar. Entonces, el contenido "HEY" se agregará en esa posición.
Mi código:
renderValue = () => { var textNode = document.createTextNode("HEY"); let p1 = document.getElementById('work').focus(); p1.appendChild(textNode) } render(){ <button onClick={() => this.renderValue()}>Paste</button> <div id="work" onClick={(e)=>e.stopPropagation()} contentEditable onInput={this.emitChange} dangerouslySetInnerHTML={{__html: this.props.html}} > </div> }Intenté agregar el texto pero no tuve suerte.
Cualquier ayuda es realmente apreciada. Gracias por adelantado.
Puede usar document.execCommand() con el comando insertText . Me gusta
document.execCommand("insertText", false, "HEY!!")