我克隆了一个存储库,该存储库专注于使用 ES6 和 Polymer 3 创建待办事项应用程序。我正在尝试实现一个按钮,该按钮在单击时将包含字符串的背景颜色变为绿色。我已经尝试过这样做,但我一直未能获得预期的结果。
示例代码:
static get properties() {
return {
list: {type: Array},
todo: {type: String},
};
}
constructor() {
super();
this.list = [
this.todoItem('buy cereal'),
this.todoItem('buy milk')
];
this.todo = '';
this.createNewToDoItem = this.createNewToDoItem.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleInput = this.handleInput.bind(this);
}
todoItem(todo) {
return {todo}
}
createNewToDoItem() {
this.list = [
...this.list,
this.todoItem(this.todo)
];
this.todo = '';
}
//Right here is where I tried to implement the background color change.
checkItem() {
checkItem = document.getElementById('checkItem'),
checkItem.addEventListener('click', () => {
this.list = this.list.filter(this.todo)
document.body.style.backgroundColor = 'green';
});
}
deleteItem(indexToDelete) {
this.list = this.list.filter((toDo, index) => index !== indexToDelete);
}
render() {
return html`
${style}
<div class="ToDo">
<h1>Grocery List</h1>
<h1 class="ToDo-Header">What do I need to buy today?</h1>
<div class="ToDo-Container">
<div class="ToDo-Content">
${repeat(
this.list,
(item, key) => {
return html`
<to-do-item
item=${item.todo}
.deleteItem=${this.deleteItem.bind(this, key)}
></to-do-item>
`;
}
)}
</div>
如果有人帮助我,我将永远感激不尽。我创建了两个 JSFiddle 链接,它们显示了我迄今为止所处理的代码:
链接 1:https : //jsfiddle.net/r2mxzp1c/(查看第 42-49 行)
链接 2:https ://jsfiddle.net/zt0x5u94/ (检查第 13 行和第 22-24 行)