简体   繁体   English

css切换无法在react.js中正常工作

[英]css toggle not working properly in react.js

Here in my app the function 'todoCompleted' is invoked when list item or checkbox is clicked. 在我的应用程序中,单击列表项或复选框时将调用函数“ todoCompleted”。 When i click the checkbox the code is not running correctly, the style is applied only once, I'm not able to toggle it. 当我单击复选框时,代码无法正确运行,该样式仅应用一次,因此无法切换。 Used : "todo.checked===true? 'line':'newtodo'" in li element. 在li元素中使用了“ todo.checked === true?'line':'newtodo'”。

  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, checked:false}) }); this.refs.inputodo.value=null; }; }; todo(todo,i){ return ( <li className={todo.checked===true? 'line':'newtodo'}> <div onClick={this.todoCompleted.bind(this,i)}> <input type="checkbox" className="option-input checkbox" checked={todo.checked} /> <div key={todo.id} className="item"> {todo.Value} <div className="Button"> <span className="destroy" onClick={this.remove.bind(this, i)}>X</span> </div> </div> </div> </li> ); }; remove(i){ this.state.todo.splice(i,1) this.setState({todo:this.state.todo}) }; todoCompleted(i){ var todo=this.state.todo; { todo[i].checked =true; this.setState({ todo:this.state.todo }); } }; allCompleted=()=>{ var todo = this.state.todo; var _this = this todo.forEach(function(item) { item.className = _this.state.finished ? "newtodo" : "line" item.checked = !_this.state.finished }) this.setState({todo: todo, finished: !this.state.finished}) }; 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.allCompleted}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')); 
  .line { text-decoration: line-through; color: red; } .newtodo{ text-decoration: none; } 
 <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> 

you are setting the state to true whenever it get click, so i replaced that to conditional value. 您会在每次单击时将状态设置为true,因此我将其替换为条件值。

todo[i].checked =todo[i].checked?false:true;

ie if set to true, then change it to false, otherwise make it true 即,如果设置为true,则将其更改为false,否则将其设置为true

 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, checked:false}) }); this.refs.inputodo.value=null; }; }; todo(todo,i){ return ( <li className={todo.checked===true? 'line':'newtodo'}> <div onClick={this.todoCompleted.bind(this,i)}> <input type="checkbox" className="option-input checkbox" checked={todo.checked} /> <div key={todo.id} className="item"> {todo.Value} <div className="Button"> <span className="destroy" onClick={this.remove.bind(this, i)}>X</span> </div> </div> </div> </li> ); }; remove(i){ this.state.todo.splice(i,1) this.setState({todo:this.state.todo}) }; todoCompleted(i){ var todo=this.state.todo; //removed curly braces as not used and added contion to set true or false todo[i].checked =todo[i].checked?false:true; this.setState({ todo:this.state.todo }); }; allCompleted=()=>{ var todo = this.state.todo; var _this = this todo.forEach(function(item) { item.className = _this.state.finished ? "newtodo" : "line" item.checked = !_this.state.finished }) this.setState({todo: todo, finished: !this.state.finished}) }; 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.allCompleted}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')); 
 .line { text-decoration: line-through; color: red; } .newtodo{ text-decoration: none; } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM