说setAttribute为null时出错JavaScript

dzhpxtsq  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(201)

我仍然是新的JS,也就是说,我试图使一个任务列表网页,用户可以提交项目,并在这些项目有一个任务列表,每个与到期日要完成。
我使用UUID v4来为项目和任务生成ID标签,但是当试图将ID标签设置为项目或任务时,setAttribute函数返回null。我有很多JS文件,我使用npm和webpack来管理它。
下面是相关的JS和HTML:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>To Do</title>
  7. </head>
  8. <body>
  9. <div id="container">
  10. <!-- project side bar panel -->
  11. <div class="proj-panel">
  12. <h3 class="proj-header">Projects</h3>
  13. <div class="proj-container">
  14. <!-- projects in here -->
  15. <div class="project">Starter Project</div>
  16. </div>
  17. <div class="add-proj-container">
  18. <div class="add-box">
  19. <span role="button" class="+ project-link" id="add-project-bttn">+</span>
  20. </div>
  21. </div>
  22. </div>
  23. <!-- main content container -->
  24. <div class="main-panel">
  25. <div class="proj-main-container">
  26. <div class="proj-info-container">
  27. <div class="proj-info">
  28. <div class="proj-info-name">Starter Project</div>
  29. <div class="proj-info-description">test description</div>
  30. </div>
  31. <div class="proj-icon-container">
  32. <div class="icon-box">
  33. <img role="button" src="../src/assests/imgs/folder-edit-outline.svg" class="proj-edit-icon">
  34. </div>
  35. <div class="icon-box">
  36. <img role="button" src="../src/assests/imgs/trash-can-outline.svg" class="proj-trash-icon">
  37. </div>
  38. </div>
  39. <!-- Need a way to edit proj info after adding -->
  40. </div>
  41. </div>
  42. <div class="task-container">
  43. <!-- daily tasks will go in here -->
  44. <div class="task">
  45. <div class="task-content">
  46. <div class="task-area">
  47. <div class="checkmark-proj"></div>
  48. <div class="task-name">Example task</div>
  49. </div>
  50. <div class="task-area">
  51. <div class="due-date"> in 0 days</div>
  52. <!-- need forum pop up on edit button -->
  53. <img src="../src/assests/imgs/pencil-outline.svg" class="edit-task">
  54. <img src="../src/assests/imgs/trash-can-outline.svg" class="delete-task">
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. <div class="add-task-container">
  60. <div class="add-box">
  61. <span role="button" class="+ project-link" id="task-add">+</span>
  62. </div>
  63. </div>
  64. </div>
  65. <!-- forum for add project & and task down here -->
  66. <div id="add-proj-container">
  67. <form class="forum-container" id="add-proj-forum">
  68. <h2>New Project</h2>
  69. <div class="input-content">
  70. <input type="text" id="proj-name" name="proj-name" required placeholder="Enter project name here">
  71. </div>
  72. <div class="forum-bttns">
  73. <button type="submit" id="add-proj-forum-bttn" class="btn">Add Project</button>
  74. <button type="button" id="close-add-proj-forum-bttn" class="btn cancel">Close</button>
  75. </div>
  76. </form>
  77. </div>
  78. <!-- Add task forum -->
  79. <div id="add-task-forum-container">
  80. <form class="forum-container" id="add-task-forum">
  81. <h2> New Task</h2>
  82. <div class="input-content">
  83. <label for="task-name">
  84. <b>Task Name</b>
  85. </label>
  86. <input type="text" id="task-name" name="task-name" required placeholder="Write the task name here...">
  87. </div>
  88. <div class="row-container">
  89. <div class="input-content">
  90. <label for="task-due-date">
  91. <b>Due Date</b>
  92. </label>
  93. <input type="date" id="task-due-date" required name="task-due-date">
  94. </div>
  95. <div class="input-content">
  96. <label for="task-project">
  97. <b>Project</b>
  98. </label>
  99. <select name="task-project" id="task-project">
  100. <option value="Inbox" selected>Inbox</option>
  101. </select>
  102. </div>
  103. <div class="forum-bttns">
  104. <button type="submit" id="add-task-forum-bttn" class="btn">Add Project</button>
  105. <button type="button" id="close-add-task-forum-bttn" class="btn cancel">Close</button>
  106. </div>
  107. </div>
  108. </form>
  109. </div>
  110. </div>
  111. </body>
  112. <script defer src="./main.js"></script>
  113. <!-- Templates to be copied -->
  114. <template id="project-template">
  115. <div class="proj-info-container">
  116. <div class="proj-info">
  117. <div class="proj-info-name"></div>
  118. <div class="proj-info-description"></div>
  119. </div>
  120. <div class="proj-icon-container">
  121. <div class="icon-box">
  122. <img role="button" src="../src/assests/imgs/folder-edit-outline.svg" class="proj-edit-icon">
  123. </div>
  124. <div class="icon-box">
  125. <img role="button" src="../src/assests/imgs/trash-can-outline.svg" class="proj-trash-icon">
  126. </div>
  127. </div>
  128. </template>
  129. <template id="task-template">
  130. <div class="task">
  131. <div class="task-content">
  132. <div class="task-area">
  133. <div class="checkmark-proj"></div>
  134. <div class="task-name">Example task</div>
  135. </div>
  136. <div class="task-area">
  137. <div class="due-date"> in 0 days</div>
  138. <!-- need forum pop up on edit button -->
  139. <img src="../src/assests/imgs/pencil-outline.svg" class="edit-task">
  140. <img src="../src/assests/imgs/trash-can-outline.svg" class="delete-task">
  141. </div>
  142. </div>
  143. </div>
  144. </template>
  145. </html>

字符串
JS索引文件:

  1. import './css/style.css'
  2. import MainModel from './sections/model/mainModel'
  3. import MainView from './sections/view/mainView'
  4. import MainController from './sections/maincontroller'
  5. const model = new MainModel()
  6. const view = new MainView()
  7. const controller = new MainController(model,view)


JS文件的问题:

  1. export default class ProjectView{
  2. constructor(element, projectModel){
  3. this.element = element
  4. this.model = projectModel
  5. this.id = projectModel.id
  6. this.name = projectModel.name
  7. }
  8. get projectName(){
  9. return this.element.querySelector('proj-name')
  10. }
  11. get id(){
  12. return this.element.getAttribute('id')
  13. }
  14. get deleteBttn(){
  15. return this.element.querySelector('proj-trash-icon')
  16. }
  17. get editBttn(){
  18. return this.element.querySelector('proj-edit-icon')
  19. }
  20. set id(value){
  21. this.element.setAttribute("id", value)
  22. }
  23. set name(value){
  24. this.projectName.textContent = value
  25. }
  26. }


在set id(value)上调用setAttribute时发生错误
其中,正在生成ID标签:

  1. import { v4 as uuidv4 } from 'uuid';
  2. export default class Project
  3. {
  4. constructor(name)
  5. {
  6. this.name = name;
  7. this.id = uuidv4();
  8. // console.log(this.id);
  9. }
  10. }


我不知道还有什么文件是必要的
错误信息:“未捕获的类型错误:Cannot read properties of null(阅读'setAttribute')”我想检索UUID生成的ID,然后将其应用于在单独的JS文件中生成的项目。

  1. import TaskView from './taskView'
  2. import ProjectView from './projectView'
  3. export default class MainView{
  4. getByID(id){
  5. return document.getElementById(id)
  6. }
  7. toggleAddTaskForumVisibility(visible, id)
  8. {
  9. const displayValue = visible ? 'block' : 'none';
  10. this.getByID(id).style.display = displayValue;
  11. }
  12. toggleFilterVisiblity(visible, id){
  13. const displayValue = visible ? 'grid' : 'none';
  14. this.getByID(id).style.display = displayValue
  15. }
  16. getAddProjectForumElementValueByName(elementName){
  17. return this.getByID('add-proj-forum').elements[elementName].value
  18. }
  19. getAddTaskForumElementValueByName(elementName){
  20. return this.getByID('add-task-forum').elements[elementName].value
  21. }
  22. resetAndCloseForumByID(forumID,forumContainerID){
  23. this.getByID(forumID).reset()
  24. this.toggleAddTaskForumVisibility(false,forumContainerID)
  25. }
  26. appendTaskItem(item){
  27. let templateContent = this.getByID('task-template').content
  28. let taskItemElementClone = templateContent.cloneNode(true).querySelector('div')
  29. let taskItemView = new TaskView(taskItemElementClone, item)
  30. this.getByID('task-container').append(taskItemView.element)
  31. return taskItemView
  32. }
  33. appendProject(item){
  34. let templateContent = this.getByID('project-template')
  35. let projectDivElementClone = templateContent.cloneNode(true).querySelector("div");
  36. let projectView = new ProjectView(projectDivElementClone, item);
  37. this.getByID('proj-container').append(projectView.element);
  38. let option = document.createElement('option')
  39. option.value = item.element
  40. option.text = item.element
  41. option.id = `checkbox-${item.id}`
  42. this.getByID('task-project').appendChild(option)
  43. return projectView
  44. }
  45. fillAddForumWhenEdit(taskItemView){
  46. this.getByID('add-task-forum').elements['task-name'].value = taskItemView.model.name
  47. this.getByID('add-task-forum').elements['task-due-date'].value = taskItemView.model.dueDate
  48. this.getByID('add-task-forum').elements['task-project'].value = taskItemView.model.project
  49. }
  50. updateTaskItem(element){
  51. let taskItemToUpdate = this.getByID(element.id)
  52. new TaskView(taskItemToUpdate,element.model)
  53. }
  54. updateTaskItemAfterProjectDeleted(element){
  55. let taskItemToUpdate = this.getByID(element.id)
  56. new TaskView(taskItemToUpdate,element)
  57. }
  58. }

vktxenjb

vktxenjb1#

在调用setAttribute之前,您可以添加一个检查,以查看this.element是否为null

  1. if (this.element) {
  2. this.element.setAttribute("id", value);
  3. } else {
  4. console.error("Element is null. Cannot set attribute.");
  5. }

字符串

相关问题