如何删除用户输入的对象JS

发布于 2025-01-28 21:27:55 字数 1004 浏览 2 评论 0原文

我正在研究一个项目,用户可以添加自己喜欢的音乐,他们也应该能够编辑和删除选项。我只能使用香草JS,我需要将SessionStorage用于此项目。我正在努力删除和编辑功能。以下是我拥有的代码,请任何指针。

在下面,我为用户输入的所有内容创建新的表元素元素

music.forEach(function (m) {
  let tableItem = document.createElement("tbody");
  tableItem.innerHTML = `<td>${m.artistName}</td> <td>${m.songName}</td> <td>${m.albumName}</td> <td>${m.genreType}</td> <button id="deleteBtn" onclick="deleteMusic()">Delete Entry</button><button id="editBtn" onclick="editMusic()">Edit Entry</button>`;
  tableItem.value = i;
  i = i + 1;
  htmlTable.appendChild(tableItem);
});

,下面是我试图用我在表上方动态创建的按钮来调用以下功能,

  music = JSON.parse(sessionStorage.getItem("favMusic"));
  sessionStorage.removeItem("favMusic", JSON.stringify(music));
}

可以在必要时发布整个脚本

如果它可以帮助我,我 使用JSON在SessionStorage上存储数据。我试图允许用户删除其创建的对象之一。因此,用户可以输入艺术家名称,歌曲和专辑名称以及类型。但是我希望用户能够单击一个按钮(这些按钮是在JS中动态创建的,也可以在表中显示),并允许他们删除或编辑DOM中的某个条目

I am working on a project where a user can add their favorite music, they should be able to edit and delete their options too. I can only use vanilla JS and I need to use sessionStorage for this project. I am struggling with the delete and edit capabilities. Below is the code I have, any pointers please.

Below I am creating new table body elements for everything that the user inputs

music.forEach(function (m) {
  let tableItem = document.createElement("tbody");
  tableItem.innerHTML = `<td>${m.artistName}</td> <td>${m.songName}</td> <td>${m.albumName}</td> <td>${m.genreType}</td> <button id="deleteBtn" onclick="deleteMusic()">Delete Entry</button><button id="editBtn" onclick="editMusic()">Edit Entry</button>`;
  tableItem.value = i;
  i = i + 1;
  htmlTable.appendChild(tableItem);
});

and below I'm trying to invoke the below function with the button that I create dynamically above in the table

  music = JSON.parse(sessionStorage.getItem("favMusic"));
  sessionStorage.removeItem("favMusic", JSON.stringify(music));
}

If it will help I can post my whole script if necessary

I am storing the data on sessionStorage using JSON. I am trying to allow the user to delete one of their created objects. So a user can input the artist name, song and album name as well as the genre. But I want the user to be able to click a button (the buttons are dynamically created in JS to display in the table as well) and allow them to delete or edit a certain entry in the DOM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文