【问题标题】:Change css of entire list on click in react单击反应更改整个列表的css
【发布时间】:2016-10-17 21:15:38
【问题描述】:

使用 react 制作了一个简单的待办事项应用。单击复选框将相应 todo 的 css 更改为删除,并在 Hover 上显示一个按钮,然后删除相应的 todo。 在这里,我想实现两件事: 1. 使用 react 在“x”上的鼠标单击事件上更改整个待办事项列表的 css。 2. 当我单击相应的列表项时,更改单个待办事项的 css。 我的应用代码是这样的。

class App extends Component {

  constructor(){
    super();
    this.state={
      todo:[]
    };
  };

  entertodo(keypress){
    var Todo=this.refs.inputodo.value;
    if( keypress.charCode == 13 )
    {
      this.setState({
        todo: this.state.todo.concat({Value:Todo, Decor:'newtodo animated fadeInLeft', checked:false})
      });
      this.refs.inputodo.value=null;
    };
  };
  todo(text,i){
    return (
      <li className={text.Decor}>
        <input type="checkbox" onChange={this.todoCompleted.bind(this,i)}className="option-input checkbox" checked={text.checked} />
        <div key={text.id}  className="item">
          {text.Value}
          <button type="button" className="destroy" onClick={this.remove.bind(this)}>X</button>
        </div>
      </li>
    );
  };

  remove(i){
    this.state.todo.splice(i,1)
    this.setState({todo:this.state.todo})
  };
  todoCompleted(i){
    var todo={...this.state.todo}
    if(todo[i].checked){
      this.state.todo[i].checked = false;
      this.state.todo[i].Decor='newtodo'
      this.setState({
        todo: this.state.todo
      });
    }
    else {
      this.state.todo[i].checked = true;
      this.state.todo[i].Decor= 'line'
      this.setState({
        todo: this.state.todo
      });
    }
  };
  **allDone(){
    this.state.todo.style= 'line'
  };**

    render() {
      return (
        <div>
          <h1 id='heading'>todos</h1>
          <div className="lines"></div>
            <div>
              <input type="text" ref= "inputodo" onKeyPress={this.entertodo.bind(this)}className="inputodo"placeholder='todos'/>
              **<span onClick={this.allDone}id="all">x</span>**
            </div>
          <div className="mainapp">
            <ul>
            {this.state.todo.map(this.todo.bind(this))}
            </ul>
          </div>
        </div>
      );
    }
  }
  export default App;

我创建了一个名为 allDone() 的函数,并使用 onClick 事件将其分配给跨元素“X”。我无法将列表中所有元素的 css 更改为删除线。

【问题讨论】:

    标签: css reactjs


    【解决方案1】:

    您可以使用 forEach() 语句添加新类或将 alldone 函数中的类更改为。还需要绑定这个函数。

    allDone= ()=>{
        var todo = this.state.todo;
    
        todo.forEach(function(item) {
          item.Decor = "newtodo animated fadeInLeft strike"
        })
        this.setState({todo: todo});
      };
    

    class App extends React.Component {
    
      constructor(){
        super();
        this.state={
          todo:[]
        };
      };
    
      entertodo(keypress){
        var Todo=this.refs.inputodo.value;
        if( keypress.charCode == 13 )
        {
          this.setState({
            todo: this.state.todo.concat({Value:Todo, Decor:'newtodo animated fadeInLeft', checked:false})
          });
          this.refs.inputodo.value=null;
        };
      };
      todo(text,i){
        return (
          <li className={text.Decor}>
            <input type="checkbox" onChange={this.todoCompleted.bind(this,i)}className="option-input checkbox" checked={text.checked} />
            <div key={text.id}  className="item">
              {text.Value}
              <button type="button" className="destroy" onClick={this.remove.bind(this)}>X</button>
            </div>
          </li>
        );
      };
    
      remove(i){
        this.state.todo.splice(i,1)
        this.setState({todo:this.state.todo})
      };
      todoCompleted(i){
        var todo={...this.state.todo}
        if(todo[i].checked){
          this.state.todo[i].checked = false;
          this.state.todo[i].Decor='newtodo'
          this.setState({
            todo: this.state.todo
          });
        }
        else {
          this.state.todo[i].checked = true;
          this.state.todo[i].Decor= 'strike'
          this.setState({
            todo: this.state.todo
          });
        }
      }; 
      allDone= ()=>{
        var todo = this.state.todo;
        
        todo.forEach(function(item) {
          item.Decor = "newtodo animated fadeInLeft strike"
        })
        this.setState({todo: todo});
      };
     
        render() {
          return (
            <div>
              <h1 id='heading'>todos</h1>
              <div className="lines"></div>
                <div>
                  <input type="text" ref= "inputodo" onKeyPress={this.entertodo.bind(this)}className="inputodo"placeholder='todos'/>
                  <span onClick={this.allDone}id="all">x</span>
                </div>
              <div className="mainapp">
                <ul>
                {this.state.todo.map(this.todo.bind(this))}
                </ul>
              </div>
            </div>
          );
        }
      }  
      ReactDOM.render(<App/>,document.getElementById('app'));
    .strike {
      text-decoration: line-through;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
    <div id="app"></div>

    【讨论】:

    • 感谢舒巴姆的帮助。当我单独单击列表项时,我还需要更改 css。
    • 我创建了与“todoCompleted”相同的函数并分配给“li”,但是当我点击 li 项目时没有任何变化
    • 我会看到的。我现在在手机上。将很快更新答案以纳入该问题
    • @GauravSoni 我认为您已经在单击复选框时实现了stike 功能,您可以在&lt;li&gt; 元素上调用 onClick 函数,然后进行类似操作
    • 我正在为同一个问题创建一个新问题并为同一个问题添加 sn-p
    【解决方案2】:

    这应该可以解决问题:

    Javascript

    allDone() {
        let new_todo = this.state.todo.map((t) => { return { Decor: 'line' } })
        this.setState({ todo: new_todo }); 
    }
    

    【讨论】:

    • 应用程序正在将复选框类型的受控输入更改为不受控制。输入元素不应从受控切换到不受控(反之亦然)。决定在组件的生命周期内使用受控输入元素还是不受控输入元素。
    • 应用程序没有错误。仅当我更改任何内容以应用新更改时才会弹出错误
    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 2021-08-12
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2020-09-20
    相关资源
    最近更新 更多