javascript - react,多个节点执行同一个事件,怎么区分启动事件的是哪个节点?
本文介绍了javascript - react,多个节点执行同一个事件,怎么区分启动事件的是哪个节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
constructor(props) {
super(props);
this.switchClick = this.switchClick.bind(this);
this.state = {
headSwitch: true
};
}
switchClick () {
//怎么区分执行switchClick()的是哪个节点
this.setState({
headSwitch: !this.state.headSwitch
});
}
render() {
return (
<div className="head-switch">
<a className={this.state.headSwitch ? 'act' : ''} onClick={this.switchClick}>1</a>
<a className={this.state.headSwitch ? '' : 'act'} onClick={this.switchClick}>2</a>
</div>
)
}
解决方案
函数加一个参数 把1 和2 传进去
< a className={this.state.headSwitch ? 'act' : ''} onClick={this.switchClick.bind(this,1)}>1</ a>
这篇关于javascript - react,多个节点执行同一个事件,怎么区分启动事件的是哪个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文