我写了一个非常简单的 reactJs 代码。我将 fontSize 设置为我的状态。我想在更改输入值时更改字体大小。但它不起作用。有人能帮忙吗?提前致谢。
这是我的js代码:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class Test extends React.Component{
constructor(props){
super(props)
this.state={
fontSize:64
}
}
changeSize(event){
this.setState({
fontSize:event.target.value
});
}
render(){
let styleobj = {background:"blue",fontSize:64}
return(
<section style={styleobj}>
<h2 className="tryout" style={{fontSize:this.state.fontSize}}>{this.state.fontSize}</h2>
<input value={this.state.fontSize} onChange={this.changeSize.bind(this)}/>
</section>
);
}
}
ReactDOM.render(<Test name="Sumei" value="123"/>,document.getElementById("root"));
最佳答案
需要指定字体大小的值(em, px, rem, % ...)
render() {
let styleobj = { background: "blue", fontSize: 64 }
return (
<section style={styleobj}>
<h2 className="tryout" style={{fontSize: this.state.fontSize+'px'}}>{this.state.fontSize}</h2>
<input value={this.state.fontSize} onChange={this.changeSize.bind(this)} />
</section>
);
}
}
关于javascript - ReactJs:从字体更改方法调用时 fontSize 不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50191700/