单击其中一个按钮时,为两个按钮切换css类
您需要跟踪类中的按钮状态。当调用onClick时,设置您的状态 onButton1Click(){单击其中一个按钮时,为两个按钮切换css类,css,reactjs,Css,Reactjs,您需要跟踪类中的按钮状态。当调用onClick时,设置您的状态 onButton1Click(){ this.setState({button1Down:true}); } 然后在渲染调用中,需要使用此状态设置要应用于按钮的类名 render(){ 让button1ClassName='button1' 如果(此.state.button1向下){ button1ClassName+=“按下按钮”; } 返回。。。 您需要跟踪类中的按钮状态。当调用onClick时,请设置您的状态 onButt
this.setState({button1Down:true});
}
this.setState({button1Down:true});
}
使用组件内部状态+库可以轻松实现:
类按钮扩展组件{
建造师(道具){
超级(道具);
this.onButtonClick=this.onButtonClick.bind(this);
此.state={
selectedButton:“类别”
}
}
ON按钮单击(e){
这是我的国家({
Selected按钮:e.target.value
});
如果(e.target.value==='categories'){
this.props.get_菜单项_api();
}否则{
this.props.get_addons_items_api();
}
}
render(){
返回(
)
}
}
使用组件内部状态+库可以轻松实现:
类按钮扩展组件{
建造师(道具){
超级(道具);
this.onButtonClick=this.onButtonClick.bind(this);
此.state={
selectedButton:“类别”
}
}
ON按钮单击(e){
这是我的国家({
Selected按钮:e.target.value
});
如果(e.target.value==='categories'){
this.props.get_菜单项_api();
}否则{
this.props.get_addons_items_api();
}
}
render(){
返回(
)
}
}
您可以保存橙色按钮组件标识符的状态,并使用onClick进行更改
组件:
class Buttons extends Component {
constructor(props) {
super(props);
this.onButtonClick = this.onButtonClick.bind(this);
this.state = {
selectedButton: 'categories'
}
}
onButtonClick(e) {
this.setState({
selectedButton: e.target.value
});
if (e.target.value === 'categories') {
this.props.get_menu_items_api();
} else {
this.props.get_addons_items_api();
}
}
render() {
return (
<div className="displayButtons">
<input className={classnames({'button': true, 'selected': this.state.selectedButton === 'categories'} onClick={onButtonClick}
value="Categories" type="button" ref="button"></input>
<input className={classnames({'button': true, 'selected': this.state.selectedButton === 'addons'})} onClick={onButtonClick}
value="Add Ons" ref="button1" type="button"></input>
</div>
)
}
}
class App extends React.Component{
constructor(){
super();
this.state = {
orangeButtonId: null
}
this.setOrangeButton = this.setOrangeButton.bind(this);
}
setOrangeButton(id){
this.setState({orangeButtonId: id});
}
render(){
return(
<div>
<input className={this.state.orangeButtonId === 1? "button1 orange" : "button1"} onClick={() => this.setOrangeButton(1)} value="Categories" type="button" ref="button" />
<input className={this.state.orangeButtonId === 2? "button2 orange" : "button2"} onClick={() => this.setOrangeButton(2)}
value="Add Ons" ref="button1" type="button" />
</div>
)
}
}
检查小提琴。您可以保存橙色按钮组件标识符的状态,并使用onClick进行更改 组件:
class Buttons extends Component {
constructor(props) {
super(props);
this.onButtonClick = this.onButtonClick.bind(this);
this.state = {
selectedButton: 'categories'
}
}
onButtonClick(e) {
this.setState({
selectedButton: e.target.value
});
if (e.target.value === 'categories') {
this.props.get_menu_items_api();
} else {
this.props.get_addons_items_api();
}
}
render() {
return (
<div className="displayButtons">
<input className={classnames({'button': true, 'selected': this.state.selectedButton === 'categories'} onClick={onButtonClick}
value="Categories" type="button" ref="button"></input>
<input className={classnames({'button': true, 'selected': this.state.selectedButton === 'addons'})} onClick={onButtonClick}
value="Add Ons" ref="button1" type="button"></input>
</div>
)
}
}
class App extends React.Component{
constructor(){
super();
this.state = {
orangeButtonId: null
}
this.setOrangeButton = this.setOrangeButton.bind(this);
}
setOrangeButton(id){
this.setState({orangeButtonId: id});
}
render(){
return(
<div>
<input className={this.state.orangeButtonId === 1? "button1 orange" : "button1"} onClick={() => this.setOrangeButton(1)} value="Categories" type="button" ref="button" />
<input className={this.state.orangeButtonId === 2? "button2 orange" : "button2"} onClick={() => this.setOrangeButton(2)}
value="Add Ons" ref="button1" type="button" />
</div>
)
}
}
检查小提琴。webpack\u require.i(…)不是函数-它给出了这个错误。webpack\u require.i(…)不是函数-它给出了这个错误。非常感谢。这太简单了。我已经为此挣扎了几个小时。我有一个疑问,这是如何将另一个按钮设置为白色的。当我已经对它应用css时。输入[type=“button”]{background color:white;}非常感谢。这太简单了。我已经为此苦苦挣扎了几个小时。我有一个疑问,这是如何将另一个按钮设置为白色的。当我已经对其应用css时。输入[type=“button”]{background color:white;}
input[type="button"]{
background-color: white;
}
input[type="button"].orange{
background-color: orange;
}