我所拥有的:
contentEditable div
反应
我想要什么:
答案 0 :(得分:0)
Contenteditable是一件棘手的事情,所以我建议您使用react-contenteditable到prevent caret jumps,加上一个简单的替换方法(您可以使用所需的任何值,例如regexp whatelse)可以达到目的:>
import React from 'react';
import ReactDOM from 'react-dom'
import ContentEditable from 'react-contenteditable'
class MyComponent extends React.Component {
state = { html: "Hello world" };
handleChange = evt => {
let html = evt.target.value
// you could take any replacement method you want
if (html.slice(-11, html.length) === 'chicken<br>') {
html = `${html.slice(0, -11)} ❤ <br>`
}
this.setState({ html });
};
render = () => {
return <ContentEditable
html={this.state.html} // innerHTML of the editable div
disabled={false} // use true to disable edition
onChange={this.handleChange} // handle innerHTML change
/>
};
};
上玩它