javascript - Javascript OOP 非常新:通过输入将值传递给属性无法呈现单个元素
问题描述
我在通过 OOP 方法渲染单个“li”元素时遇到问题。
我正在从用户那里获取输入并使用此信息通过一个类创建一个项目。然后我将这个类连接到负责呈现列表的列表类。
一旦我通过单击事件侦听器获取值,singleTaskRendering 类就不起作用。我想知道我是否设置不正确?
const inputAccess = document.querySelector('.control').querySelector('input');
const addItemBtnAccess = document.getElementById('add-item-btn');
const toDoList = [];
const doneList = [];
//ads a li id to the item
const idGenerator = (array) => {
let n = 1;
let message = '';
for (let i = 0; i < array.length; i++) {
n += 1;
}
message = `li-${n}`;
return message;
}
class ItemTask {
constructor(itemValue, idGen) {
this.title = itemValue;
this.id = idGen;
}
}
const addItemBtnHandler = () => {
const toDoList = [];
const inputValue = inputAccess.value;
const item= new ItemTask(inputValue, idGenerator(toDoList));
toDoList.push(item);
return toDoList;
};
class singleTaskRendering {
constructor(product) {
this.product = product;
}
render() {
const titleElement = document.createElement('div');
titleElement.id = this.product.id;
titleElement.innerHTML = `
<li>
<h2>${this.product.title}</h2>
<button>Done</button>
<button>Delete</button>
</li>`;
titleElement.draggable = true;
}
}
class ItemLists {
constructor(listId, items) {
this.items = items;
this.listId = listId;
console.log(this.items, this.listId);
}
renderList() {
const renderHook = document.getElementById('hook');
const createList = document.createElement('lu');
createList.className = 'card';
createList.id = `${this.listId}-list`;
for(let item of this.items) {
const newItem = new singleTaskRendering(item);
console.log(newItem);
const itemEl = newItem.render();
console.log(itemEl, newItem);
createList.apppend(itemEl);
}
renderHook.append(createList);
}
}
const itemList = new ItemLists('active', toDoList);
itemList.renderList();
addItemBtnAccess.addEventListener('click', addItemBtnHandler);
解决方案
您遇到的问题是您ItemLists
在页面加载时调用,这意味着它只会处理一个空的toDoList
.
我的解决方案是将 renderList 重命名为 appendItem。
在顶部声明它
不要将列表 id 和列表传递给构造函数,而是将其传递给 clickhandler 中的 appendItem。
const inputAccess = document.querySelector('.control').querySelector('input'); const addItemBtnAccess = document.getElementById('add-item-btn'); const itemList = new ItemLists(); const toDoList = []; const doneList = []; //ads a li id to the item const idGenerator = (array) => { let n = 1; let message = ''; for (let i = 0; i < array.length; i++) { n += 1; } message = `li-${n}`; return message; } class ItemTask { constructor(itemValue, idGen) { this.title = itemValue; this.id = idGen; } } const addItemBtnHandler = () => { const toDoList = []; const inputValue = inputAccess.value; const item= new ItemTask(inputValue, idGenerator(toDoList)); itemList.appendItem('active', item); }; class singleTaskRendering { constructor(product) { this.product = product; } render() { const titleElement = document.createElement('div'); titleElement.id = this.product.id; titleElement.innerHTML = ` <li> <h2>${this.product.title}</h2> <button>Done</button> <button>Delete</button> </li>`; titleElement.draggable = true; } } class ItemLists { appendItem(listId, item) { const renderHook = document.getElementById('hook'); const createList = document.createElement('lu'); createList.className = 'card'; createList.id = `${listId}-list`; const newItem = new singleTaskRendering(item); console.log(newItem); const itemEl = newItem.render(); console.log(itemEl, newItem); createList.apppend(itemEl); renderHook.append(createList); } } addItemBtnAccess.addEventListener('click', addItemBtnHandler);
推荐阅读
- python - 在表单中使用 MultipleChoiceField 作为 Integerfield
- xamarin.forms - Xamarin Forms 主题源
- python - 如何使用 tensorflow 编写这个函数?
- rest - 如果在发布子资源时资源不存在,则在 REST API 中使用哪个 HTTP 状态代码?
- php - PHP 会话变量在重定向时不可用
- amazon-s3 - 如何恢复在二进制文件中复制的 S3 的本地备份
- c# - 即使安装了 .NET v.4.0,HttpClient 也会导致“System.Net.Http”程序集未找到异常
- javascript - 我的 CSS 动画不起作用
- vba - 确认所选邮件项目在特定文件夹中
- javascript - 图 DFS 运行缓慢