【发布时间】: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 更改为删除线。
【问题讨论】: