有一个待办事项列表任务。有三个组成部分。 class TodoAppComponent,类TodoFormComponent和类TodoListComponent。请在TodoListComponent中检查我的doneTask()。当我添加一个任务时,我需要越过行,当复选框状态变为“已检查”时,它似乎无法正常工作,但控制台不显示任何错误
class TodoAppComponent {
constructor(mountPoint, props = {}) {
this.mountPoint = mountPoint;
this.props = props;
}
querySelectors() {
this.todoFormMountPoint = this.mountPoint.querySelector(
".todo-app__form-point"
);
this.todoListMountPoint = this.mountPoint.querySelector(
".todo-app__list-point"
);
}
mountChildren() {
this.todoFormComponent = new TodoFormComponent(this.todoFormMountPoint, {
onTodoAdd: this.handleTodoAdd.bind(this)
});
this.todoFormComponent.mount();
this.todoListComponent = new TodoListComponent(this.todoListMountPoint);
this.todoListComponent.mount();
}
handleTodoAdd(task) {
this.todoListComponent.addTask(task);
const numItems = this.todoListComponent.getNumTasks();
this.todoFormComponent.setCounter(numItems + 1);
}
mount() {
this.mountPoint.innerHTML = this.render();
this.querySelectors();
this.mountChildren();
}
render() {
return `
<div class="todo-app">
<h2>Todo:</h2>
<div class="todo-app__form-point"></div>
<div class="todo-app__list-point"></div>
</div>
`;
}
}
class TodoFormComponent {
constructor(mountPoint, props = {}) {
this.mountPoint = mountPoint;
this.props = props;
}
querySelectors() {
this.field = this.mountPoint.querySelector(".todo-form__field");
this.btn = this.mountPoint.querySelector(".todo-form__btn");
this.counter = this.mountPoint.querySelector(".todo-form__counter");
}
addEventListeners() {
this.btn.addEventListener("click", this.handeBtnClick.bind(this));
}
handeBtnClick() {
this.props.onTodoAdd(this.field.value);
this.field.value = "";
}
mount() {
this.mountPoint.innerHTML = this.render();
this.querySelectors();
this.addEventListeners();
}
setCounter(val) {
this.counter.innerHTML = val;
}
render() {
return `
<div class="todo-form">
<input class="todo-form__field" type="text">
<button class="todo-form__btn">
Add #<span class="todo-form__counter">1</span>
</button>
</div>
`;
}
}
class TodoListComponent {
constructor(mountPoint, props = {}) {
this.mountPoint = mountPoint;
this.props = props;
}
querySelectors() {
this.list = this.mountPoint.querySelector(".todo-list");
}
mount() {
this.mountPoint.innerHTML = this.render();
this.querySelectors();
this.doneTask();
}
addTask(task) {
this.list.innerHTML += `
<tr><td><input class ="checkbox" type="checkbox"></td>
<td>${task}</td>
<td><button value="×" style="width:5px;height:15px;"></button></td></tr>
`;
}
doneTask() {
let checkboxElements = document.getElementsByClassName("checkbox");
for (let i = 0; i < checkboxElements.length; i++) {
checkboxElements[i].addEventListener("change", function() {
if (this.checked) {
this.closest("tr").style.textDecoration = "line-through";
}
});
}
}
getNumTasks() {
return this.list.children.length;
}
render() {
return `
<br>
<table class="todo-list" style="width:50px"></table>
`;
}
}
const root = document.querySelector("#root");
const cmp = new TodoAppComponent(root);
cmp.mount();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
class TodoAppComponent {
constructor(mountPoint, props = {}) {
this.mountPoint = mountPoint;
this.props = props;
}
querySelectors() {
this.todoFormMountPoint = this.mountPoint.querySelector(
".todo-app__form-point"
);
this.todoListMountPoint = this.mountPoint.querySelector(
".todo-app__list-point"
);
}
mountChildren() {
this.todoFormComponent = new TodoFormComponent(this.todoFormMountPoint, {
onTodoAdd: this.handleTodoAdd.bind(this)
});
this.todoFormComponent.mount();
this.todoListComponent = new TodoListComponent(this.todoListMountPoint);
this.todoListComponent.mount();
}
handleTodoAdd(task) {
this.todoListComponent.addTask(task);
const numItems = this.todoListComponent.getNumTasks();
this.todoFormComponent.setCounter(numItems + 1);
}
mount() {
this.mountPoint.innerHTML = this.render();
this.querySelectors();
this.mountChildren();
}
render() {
return `
<div class="todo-app">
<h2>Todo:</h2>
<div class="todo-app__form-point"></div>
<div class="todo-app__list-point"></div>
</div>
`;
}
}
class TodoFormComponent {
constructor(mountPoint, props = {}) {
this.mountPoint = mountPoint;
this.props = props;
}
querySelectors() {
this.field = this.mountPoint.querySelector(".todo-form__field");
this.btn = this.mountPoint.querySelector(".todo-form__btn");
this.counter = this.mountPoint.querySelector(".todo-form__counter");
}
addEventListeners() {
this.btn.addEventListener("click", this.handeBtnClick.bind(this));
}
handeBtnClick() {
this.props.onTodoAdd(this.field.value);
this.field.value = "";
}
mount() {
this.mountPoint.innerHTML = this.render();
this.querySelectors();
this.addEventListeners();
}
setCounter(val) {
this.counter.innerHTML = val;
}
render() {
return `
<div class="todo-form">
<input class="todo-form__field" type="text">
<button class="todo-form__btn">
Add #<span class="todo-form__counter">1</span>
</button>
</div>
`;
}
}
class TodoListComponent {
constructor(mountPoint, props = {}) {
this.mountPoint = mountPoint;
this.props = props;
}
querySelectors() {
this.list = this.mountPoint.querySelector(".todo-list");
}
mount() {
this.mountPoint.innerHTML = this.render();
this.querySelectors();
this.doneTask();
}
addTask(task) {
this.list.innerHTML += `
<tr><td><input class ="checkbox" type="checkbox"></td>
<td>${task}</td>
<td><button value="×" style="width:5px;height:15px;"></button></td></tr>
`;
this.doneTask();
}
doneTask() {
let checkboxElements = document.getElementsByClassName("checkbox");
for (let i = 0; i < checkboxElements.length; i++) {
checkboxElements[i].addEventListener("change", function() {
if (this.checked) {
this.closest("tr").style.textDecoration = "line-through";
}
});
}
}
getNumTasks() {
return this.list.children.length;
}
render() {
return `
<br>
<table class="todo-list" style="width:50px"></table>
`;
}
}
const root = document.querySelector("#root");
const cmp = new TodoAppComponent(root);
cmp.mount();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
因为你的函数doneTask()
没有被调用,所以这就是样式没有更新的原因。试试这个。我刚刚在doneTask()
打电话给addTask()
。
希望这可以帮助!!
方法doneTask()
在页面初始化时只运行一次,以添加侦听器。此时实际上没有元素,因此没有添加任何侦听器。
相反,尝试为添加到DOM的每个元素添加一个侦听器(因此doneTask()
是多余的):
addTask(task) {
this.list.innerHTML += `
<tr><td><input class ="checkbox" type="checkbox"></td>
<td>${task}</td>
<td><button value="×" style="width:5px;height:15px;"></button></td></tr>
`;
// Actually add the listener
this.list.lastChild.firstChild.firstChild.firstChild.addEventListener("change", function() {
if (this.checked) {
this.closest("tr").style.textDecoration = "line-through";
}
});
}