我希望我的handleChange函数将输入的值更改为下标。在React中怎么做?
class App extends Component {
state = {
equation: ""
};
handleChange = event => {
this.setState({ [event.target.name]: event.target.value });
};
render() {
const { equation } = this.state;
console.log(equation);
return (
<div>
<input onChange={this.handleChange} value={equation} name="equation" />
</div>
);
}
}
答案 0 :(得分:2)
正则表达式选择+捕获数字字符,并替换为相应的下标html实体:
handleChange = event => {
const formattedValue = event.target.value.replace(/(\d)/g, '̀$1;');
this.setState({ [event.target.name]: formattedValue });
};