我仍然是新的JS,也就是说,我试图使一个任务列表网页,用户可以提交项目,并在这些项目有一个任务列表,每个与到期日要完成。
我使用UUID v4来为项目和任务生成ID标签,但是当试图将ID标签设置为项目或任务时,setAttribute函数返回null。我有很多JS文件,我使用npm和webpack来管理它。
下面是相关的JS和HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To Do</title>
</head>
<body>
<div id="container">
<!-- project side bar panel -->
<div class="proj-panel">
<h3 class="proj-header">Projects</h3>
<div class="proj-container">
<!-- projects in here -->
<div class="project">Starter Project</div>
</div>
<div class="add-proj-container">
<div class="add-box">
<span role="button" class="+ project-link" id="add-project-bttn">+</span>
</div>
</div>
</div>
<!-- main content container -->
<div class="main-panel">
<div class="proj-main-container">
<div class="proj-info-container">
<div class="proj-info">
<div class="proj-info-name">Starter Project</div>
<div class="proj-info-description">test description</div>
</div>
<div class="proj-icon-container">
<div class="icon-box">
<img role="button" src="../src/assests/imgs/folder-edit-outline.svg" class="proj-edit-icon">
</div>
<div class="icon-box">
<img role="button" src="../src/assests/imgs/trash-can-outline.svg" class="proj-trash-icon">
</div>
</div>
<!-- Need a way to edit proj info after adding -->
</div>
</div>
<div class="task-container">
<!-- daily tasks will go in here -->
<div class="task">
<div class="task-content">
<div class="task-area">
<div class="checkmark-proj"></div>
<div class="task-name">Example task</div>
</div>
<div class="task-area">
<div class="due-date"> in 0 days</div>
<!-- need forum pop up on edit button -->
<img src="../src/assests/imgs/pencil-outline.svg" class="edit-task">
<img src="../src/assests/imgs/trash-can-outline.svg" class="delete-task">
</div>
</div>
</div>
</div>
<div class="add-task-container">
<div class="add-box">
<span role="button" class="+ project-link" id="task-add">+</span>
</div>
</div>
</div>
<!-- forum for add project & and task down here -->
<div id="add-proj-container">
<form class="forum-container" id="add-proj-forum">
<h2>New Project</h2>
<div class="input-content">
<input type="text" id="proj-name" name="proj-name" required placeholder="Enter project name here">
</div>
<div class="forum-bttns">
<button type="submit" id="add-proj-forum-bttn" class="btn">Add Project</button>
<button type="button" id="close-add-proj-forum-bttn" class="btn cancel">Close</button>
</div>
</form>
</div>
<!-- Add task forum -->
<div id="add-task-forum-container">
<form class="forum-container" id="add-task-forum">
<h2> New Task</h2>
<div class="input-content">
<label for="task-name">
<b>Task Name</b>
</label>
<input type="text" id="task-name" name="task-name" required placeholder="Write the task name here...">
</div>
<div class="row-container">
<div class="input-content">
<label for="task-due-date">
<b>Due Date</b>
</label>
<input type="date" id="task-due-date" required name="task-due-date">
</div>
<div class="input-content">
<label for="task-project">
<b>Project</b>
</label>
<select name="task-project" id="task-project">
<option value="Inbox" selected>Inbox</option>
</select>
</div>
<div class="forum-bttns">
<button type="submit" id="add-task-forum-bttn" class="btn">Add Project</button>
<button type="button" id="close-add-task-forum-bttn" class="btn cancel">Close</button>
</div>
</div>
</form>
</div>
</div>
</body>
<script defer src="./main.js"></script>
<!-- Templates to be copied -->
<template id="project-template">
<div class="proj-info-container">
<div class="proj-info">
<div class="proj-info-name"></div>
<div class="proj-info-description"></div>
</div>
<div class="proj-icon-container">
<div class="icon-box">
<img role="button" src="../src/assests/imgs/folder-edit-outline.svg" class="proj-edit-icon">
</div>
<div class="icon-box">
<img role="button" src="../src/assests/imgs/trash-can-outline.svg" class="proj-trash-icon">
</div>
</div>
</template>
<template id="task-template">
<div class="task">
<div class="task-content">
<div class="task-area">
<div class="checkmark-proj"></div>
<div class="task-name">Example task</div>
</div>
<div class="task-area">
<div class="due-date"> in 0 days</div>
<!-- need forum pop up on edit button -->
<img src="../src/assests/imgs/pencil-outline.svg" class="edit-task">
<img src="../src/assests/imgs/trash-can-outline.svg" class="delete-task">
</div>
</div>
</div>
</template>
</html>
字符串
JS索引文件:
import './css/style.css'
import MainModel from './sections/model/mainModel'
import MainView from './sections/view/mainView'
import MainController from './sections/maincontroller'
const model = new MainModel()
const view = new MainView()
const controller = new MainController(model,view)
型
JS文件的问题:
export default class ProjectView{
constructor(element, projectModel){
this.element = element
this.model = projectModel
this.id = projectModel.id
this.name = projectModel.name
}
get projectName(){
return this.element.querySelector('proj-name')
}
get id(){
return this.element.getAttribute('id')
}
get deleteBttn(){
return this.element.querySelector('proj-trash-icon')
}
get editBttn(){
return this.element.querySelector('proj-edit-icon')
}
set id(value){
this.element.setAttribute("id", value)
}
set name(value){
this.projectName.textContent = value
}
}
型
在set id(value)上调用setAttribute时发生错误
其中,正在生成ID标签:
import { v4 as uuidv4 } from 'uuid';
export default class Project
{
constructor(name)
{
this.name = name;
this.id = uuidv4();
// console.log(this.id);
}
}
型
我不知道还有什么文件是必要的
错误信息:“未捕获的类型错误:Cannot read properties of null(阅读'setAttribute')”我想检索UUID生成的ID,然后将其应用于在单独的JS文件中生成的项目。
import TaskView from './taskView'
import ProjectView from './projectView'
export default class MainView{
getByID(id){
return document.getElementById(id)
}
toggleAddTaskForumVisibility(visible, id)
{
const displayValue = visible ? 'block' : 'none';
this.getByID(id).style.display = displayValue;
}
toggleFilterVisiblity(visible, id){
const displayValue = visible ? 'grid' : 'none';
this.getByID(id).style.display = displayValue
}
getAddProjectForumElementValueByName(elementName){
return this.getByID('add-proj-forum').elements[elementName].value
}
getAddTaskForumElementValueByName(elementName){
return this.getByID('add-task-forum').elements[elementName].value
}
resetAndCloseForumByID(forumID,forumContainerID){
this.getByID(forumID).reset()
this.toggleAddTaskForumVisibility(false,forumContainerID)
}
appendTaskItem(item){
let templateContent = this.getByID('task-template').content
let taskItemElementClone = templateContent.cloneNode(true).querySelector('div')
let taskItemView = new TaskView(taskItemElementClone, item)
this.getByID('task-container').append(taskItemView.element)
return taskItemView
}
appendProject(item){
let templateContent = this.getByID('project-template')
let projectDivElementClone = templateContent.cloneNode(true).querySelector("div");
let projectView = new ProjectView(projectDivElementClone, item);
this.getByID('proj-container').append(projectView.element);
let option = document.createElement('option')
option.value = item.element
option.text = item.element
option.id = `checkbox-${item.id}`
this.getByID('task-project').appendChild(option)
return projectView
}
fillAddForumWhenEdit(taskItemView){
this.getByID('add-task-forum').elements['task-name'].value = taskItemView.model.name
this.getByID('add-task-forum').elements['task-due-date'].value = taskItemView.model.dueDate
this.getByID('add-task-forum').elements['task-project'].value = taskItemView.model.project
}
updateTaskItem(element){
let taskItemToUpdate = this.getByID(element.id)
new TaskView(taskItemToUpdate,element.model)
}
updateTaskItemAfterProjectDeleted(element){
let taskItemToUpdate = this.getByID(element.id)
new TaskView(taskItemToUpdate,element)
}
}
型
1条答案
按热度按时间vktxenjb1#
在调用setAttribute之前,您可以添加一个检查,以查看
this.element
是否为null
。字符串