我有一个简单的笔记web 5应用程序.每次用户单击"+"按钮时,都会添加一个新注释.一旦他点击'⟳' 按钮全部排除.我想在用户单击的 sticky 中添加五个新项目槽.为了做到这一点,我需要知道他是怎么做的.这最后一点让我很困惑.如果所有按钮都是由用户生成的,我如何知道用户单击了哪个按钮?

HTML:

<html lang="en">
  <head>
    <title>To Do Lists</title>
    <link rel="stylesheet" href="styles.css" />
    <script src="script.js"></script>
  </head>
  <body>
    <div class="top_bar">
      <button id="plus">+</button>
      <button id="restart">⟳</button>
    </div>
    <div id="notes" class="notes">

    </div>
  </body>
</html>

CSS

    padding: 0px;
    margin: 0px;
}
body{
    display: flex;
    flex-direction: column;
}
.top_bar{
    width: 100%;
    height: 10vh;
    background-color: #95E29B;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
button{
    font-size: 35px;
    border: none;
    width: 15%;
    height: 10vh;
    background-color: #3BCE4B;
    cursor: pointer;
}
.notes{
    width: 100%;
    height: 90vh;
    overflow: auto;
    background-color: black;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
}

.note{
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 20px;
    height: 40vh;
    width: 30%;
    border-radius: 10px;
    background-color: white;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: flex-end;

}

.note_input{
    margin-top: 20px;
    margin-right: 5%;
    font-size: 30px;
    width: 90%;
    border-style: solid;
    border-top: none;
    border-left: none;
    border-right: none;
    border-color: black;
}

form{
    margin-top: 10px;
    margin-right: 15%;
    width: 80%;
    height: 49%;
    overflow-y: auto;
}

li{
    border: none;
    width: 70%;
    display: flex;
}

.li_input{
    border-style: solid;
    border-color: black;
    border-top: none;
    border-left: none;
    border-right: none;
    margin-left: 10px;
    font-size: 20px;
}

.more_items{
    width: 35px;
    height: 35px;
    margin-right: 2%;
    border-radius: 100%;
    font-size: 20px;
}

JavaScript

const add_note = () => {
  // Creates a new note and its props
  const new_note = document.createElement("div");
  const new_input = document.createElement("input");
  const new_form = document.createElement("form");
  const new_ol = document.createElement("ol");
  const new_button = document.createElement("button");

  //Populates the new note with inputs and checkboxes
  for (let i = 0; i < 5; i++) {
    let new_li = document.createElement("li");
    let new_checkbox = document.createElement("input");
    new_checkbox.setAttribute("type", "checkbox");
    let new_li_input = document.createElement("input");
    new_li_input.classList.add("li_input");
    new_ol.appendChild(new_li);
    new_li.appendChild(new_checkbox);
    new_li.appendChild(new_li_input);
  }

  //New note's settings
  new_note.classList.add("note");
  new_note.appendChild(new_input);
  new_input.classList.add("note_input");
  new_input.setAttribute("placeholder", "Note's title");
  new_note.appendChild(new_form);
  new_ol.classList.add("ols");
  new_form.appendChild(new_ol);
  new_note.appendChild(new_button);
  new_button.classList.add("more_items");

  //Inserts the new note and button
  const note_block = document.getElementById("notes");
  note_block.appendChild(new_note);
  new_button.addEventListener("click", add_more_items);
  new_button.innerHTML = "+";
};

//Adds more items
const add_more_items = () => {
  //console.log(new_button.parentElement.nodeName);
  //let new_ol = document.getElementsByClassName("ols")[];
  for (let i = 0; i < 5; i++) {
    let new_li = document.createElement("li");
    let new_checkbox = document.createElement("input");
    new_checkbox.setAttribute("type", "checkbox");
    let new_li_input = document.createElement("input");
    new_li_input.classList.add("li_input");
    new_ol.appendChild(new_li);
    new_li.appendChild(new_checkbox);
    new_li.appendChild(new_li_input);
  }
};

//Removes all notes
const remove_note = () => {
  let amount_of_notes = document.getElementsByClassName("note").length;
  console.log(amount_of_notes);
  while (amount_of_notes != 0) {
    amount_of_notes--;
    document.getElementsByClassName("note")[amount_of_notes].remove();
  }
  alert("All notes were removed.");
};

// Loads the buttons
const load_buttons = () => {
  document.getElementById("plus").addEventListener("click", add_note);
  document.getElementById("restart").addEventListener("click", remove_note);
};

// Main method
document.addEventListener("DOMContentLoaded", () => {
  load_buttons();
});

提前谢谢.

推荐答案

考虑到html相当简单,您可以使用parentNode以基本的风格完成这项工作.

您当前的代码出错,因为您试图以new_ol为目标添加新的<li>个元素,但它不在add_more_items函数的范围内.即使它这样做了,它也会模棱两可——它应该指哪一个<ol>

相反,您可以通过单击的按钮计算出父对象<ol>,如下所示:

const add_more_items = (e) => {
  const new_ol = e.target.parentNode.querySelector('ol');
  // rest of your code
}

这里有一个完整的片段,将所有这些放在一起.我把它放在了一个代码笔中,因为这里的代码段编辑器正在努力处理布局的某些部分:https://codepen.io/29b6/pen/qBxxXqG

请记住,这样遍历DOM并不理想.这种方法的主要问题是,如果HTML struct 发生变化,最终可能会将多个parentNode链接在一起,这很快就会变得很糟糕.但它应该帮助您理解按照您的要求 Select 元素的父元素的概念.

Javascript相关问答推荐

脚本.js:3:20未捕获的类型错误:无法读取空的属性(读取addEventHandler)

RxJS setTimeout操作符等效

使用TMS Web Core中的HTML模板中的参数调用过程

colored颜色 检测JS,平均图像 colored颜色 检测JS

成功完成Reducers后不更新状态

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

Html文件和客户端存储的相关问题,有没有可能?

在WordPress中使用带有WPCode的Java代码片段时出现意外令牌错误

rxjs插入延迟数据

单个HTML中的多个HTML文件

如何发送从REST Api收到的PNG数据响应

使用VUE和SCSS的数字滚动动画(&;内容生成)

如果一个字符串前面有点、空格或无字符串,后面有空格、连字符或无字符串,则匹配正则表达式

未捕获的运行时错误:调度程序为空

为什么在函数中添加粒子的速率大于删除粒子的速率?

有没有一种直接的方法可以深度嵌套在一个JavaScript对象中?

FileReader()不能处理Firefox和GiB文件

JavaScript -复制到剪贴板在Windows计算机上无效

在没有任何悬停或其他触发的情况下连续交换图像

如何使用fltter_js将对象作为参数传递给javascrip?