摆弄 html 和 javascript: https://jsfiddle.net/4a4u1jg1/
- 我希望使用“.finished”类保存在数组中的元素与数组中的类一起保存,如果我再次点击该元素并删除该类,则它们应该在不包含类的情况下保存数组,我该如何实现这个?
2a。当我双击一个项目时,它会删除屏幕上的正确项目,但在我重新加载应用程序后,它会删除列表中的最后一个项目,换句话说,它会从 html 中删除正确的项目,但会删除数组中的最后一个项目。 解决了!
2b。此外,我无法删除新添加的项目(或标记为已完成),我必须重新打开/更新应用程序/浏览器。
$("li").dblclick(function()
{
//Removes last task instead of the task I double tapped on
//and I can't remove newly added tasks
taskListArray.splice($.inArray($(this), taskListArray, 1));
$(this).remove();
if(window.localStorage)
{
window.localStorage.setItem("taskList", JSON.stringify(taskListArray));
}
});
如果有人能帮助我解决问题 #1 和 #2b,那就太好了,谢谢!
最佳答案
尝试使用 <li>
拼接的索引,因为实际元素不是数组中的元素:
改变:
taskListArray.splice($.inArray($(this), taskListArray, 1));
到
taskListArray.splice($(this).index(),1);
另请注意 splice() 的第三个参数用于添加到数组
关于javascript - 在数组中保存类+拼接删除错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43352936/