如何在2个元素中切换活动类

时间:2019-06-13 18:10:30

标签: javascript reactjs

我试图在单击卡片时切换活动类,出于某种原因,它仅对我拥有的第一个div有效,而对第二个div元素无效。基本上,当您单击div元素时,它应该删除box类并添加活动类,但是当我单击第二个div时,它会影响第一个div元素的样式。非常感谢您的帮助!

class RegisterForm extends React.Component {


  constructor(props) {

    super(props);
    this.handleClick= this.handleClick.bind(this);

    this.handleClick2= this.handleClick.bind(this);
    this.state = {
      active: false,
      active2: false
  };

  }

  handleClick(){

  this.setState({ active: !this.state.active });
  console.log(this.state.active);

  }


  handleClick2(){

    this.setState({ active2: !this.state.active2 });
    console.log(this.state.active2);

  }
  render() {
    const { form } = this.props

    return (
      <div>
        {' '}
        <h1 id={styles.n}> Get started Today </h1>
        <h5 id="f">No credit card required </h5>
        <br />
        <br />

        <div className="container">
          <div className={this.state.active ? styles.active : styles.box} onClick={this.handleClick} onKeyDown={this.handleClick} role="presentation">
            <span className={styles.cont}>
              <i className="fa fa-rocket" />.
            </span>

            <h3>Organization</h3>

            <p className={styles.expand}>
              <span className="plus">full access to all settings</span>
              <span className="minus">-</span>
            </p>
          </div>

          <div className={this.state.active2 ? styles.active : styles.box} onClick={this.handleClick2}>
            <span className={styles.cont}>
              <i className="fa fa-edit" />.
            </span>

}

1 个答案:

答案 0 :(得分:0)

您正在将this.handleClick函数绑定到构造函数中的this.handleClick2