- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
本质上,我将 notes[]
数组中的一些笔记渲染到我的页面,并为每个笔记添加一个按钮,单击该按钮时,将使用 splice()
,然后重新渲染剩下的音符。该按钮根据笔记的标题(由用户输入)赋予一个 id,它也被赋予一个类似的 id。
这只是个人项目的一些 Vanilla JS,今天早上环顾四周后,我还没有找到不处理 Vue、Angular 或其他一些框架/包的解决方案。
这是相关的 HTML:
<main>
<div id="container">
<form id="utilities">
<input type="text" name="userTitle" id="user-title" placeholder="Title" autocomplete="off">
<input type="text" name="userBody" id="user-body" placeholder="Body" autocomplete="off">
<div id="buttons">
<input type="submit" name="userSubmit" id="user-submit" value="Submit">
</div>
<input type="text" name="userFilter" id="user-filter" placeholder="Search" autocomplete="off">
</form>
<div id="results"></div>
</div>
</main>
以下 JS 片段都是按顺序排列的,只是为了便于阅读和评论而进行了拆分。
这是我要传递到和从中提取的数组。它需要标题、正文和 ID。
const notes = [{
title: 'Grocery List',
body: 'Buy milk, eggs, and bread',
id: 'grocery-list'
},
{
title: 'Workout Routine',
body: 'running, push-ups, rowing',
id: 'workout-routine'
}
]
这是我的函数,它从用户输入中获取标题和正文文本。它在 renderNotes()
中调用。
const results = document.querySelector('#results');
const createElement = function(noteTitle, noteBody) {
const newNote = document.createElement('div');
const newTitle = document.createElement('h3');
const newBody = document.createElement('p');
const newButton = document.createElement('div');
newTitle.textContent = noteTitle;
newTitle.classname = 'note-title';
newBody.textContent = noteBody;
newBody.className = 'note-body';
newButton.className = 'note-button';
newButton.innerHTML = '⮿';
newButton.id = `button-${newTitle.textContent.toLowerCase()}`.replace(' ', '-');
newNote.appendChild(newTitle);
newNote.appendChild(newButton);
newNote.appendChild(newBody);
newNote.className = "note";
newNote.id = newTitle.textContent.toLowerCase().replace(' ', '-');
results.appendChild(newNote);
}
renderNotes()
只是对该数组中的每个音符调用 createElement()
。然后我第一次调用它。
const renderNotes = function(list) {
console.log(list)
list.forEach(function(item) {
createElement(item.title, item.body)
})
}
renderNotes(notes);
这段代码似乎是我失败的地方。它应该获取每个按钮(在 createElement()
调用期间创建)并向其添加 eventListener('click')
。然后它应该查看 notes[]
中的每个笔记,找到与按下的按钮具有相似 id 的那个,然后 splice()
来自 notes[]
数组的注释。然后我只想重新渲染笔记。
document.querySelectorAll('.note-button').forEach(function(button) {
button.addEventListener('click', function(e) {
notes.forEach(function(note) {
if (e.target.id.includes(note.id)) {
notes.splice(notes.indexOf(note), 1)
}
renderNotes(notes)
})
})
});
笔记似乎是从数组中一致拼接出来的,但我的问题在于让它们重新渲染。目前它只是删除项目,然后无法呈现或正在向页面添加更多副本。 编辑添加: 我在控制台中没有收到任何错误,代码只是在计算机方面正确执行。所以我知道这绝对是“椅子和键盘之间的问题”错误。
希望这是有道理的。感谢您的回复。
最佳答案
您在每次迭代时调用 renderNotes(notes)
,而不仅仅是在数组发生变化之后。
尝试这样的事情:
const clickedNote = notes.find(note => e.target.id.includes(note.id));
notes.splice(notes.indexOf(clickedNote), 1);
renderNotes(notes);
更好的解决方案是使用 let
定义 notes
,然后执行如下操作:
notes = notes.filter(note => !e.target.id.includes(note.id));
renderNotes(notes);
关于JavaScript - 在拼接数组中的项目然后将该数组重新呈现到我的页面时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56308180/
.splice() 方法的基本问题,以及如何最好地从数组中删除元素。 我想使用 .splice() 从数组中删除一个项目,但是当我这样做时,我希望返回原始数组 减去 删除的元素。 .splice()
我正在过滤同位素库。单击按钮会添加到称为过滤器的数组中。这是正常工作的,但是当用户单击事件按钮时,我希望从数组中删除过滤的类别。由于某种原因,事实并非如此。 这是特定的代码: if($(this).h
几个月前,我将一个 Subversion 存储库转换为 Mercurial,结果在我的修订历史中留下了两个毫无意义的空白。我试图弄清楚我是否可以拼接间隙,但我一直无法让工具精确地完成我想要的。 在项目
我制作简单的射击游戏,但有一个问题(有时): GIF Game problem 3颗子弹被移除(但只有一颗子弹相撞)。子弹和敌人(蓝色方 block )位于一个数组中(var objects = []
假设我们在 JavaScript 中有两个数组,[3,4,7] 和 [5,6]。 在不排序或使用 .apply 的情况下,将 [5,6] 插入索引 2 处的 [3,4,7] 的最佳方式是什么实现结果数
我试图在从数组中删除项目时添加纸质警报对话框,但我似乎无法理解这是如何工作的。 我现在可以使用以下功能,无需对话框: _delete(e) { var index = this.t
我正在尝试使用 splice() 从 jQuery 对象中删除元素. 但是,最终发生的情况是所有其他项目都被删除。我假设这是由于使用 splice 重新索引造成的。 我要淡入每个 所以我需要从顶部开始
这是我尝试从数组中动态删除一个值的尝试 $('.btn-remove').click(function() { var players = ["compare","13076","13075"
我正在考虑在 Visual Studio 2008 上使用 OpenCV 2.3.1 实时拼接来自 2 个或更多(目前可能是 3 或 4 个)摄像头的图像。 但是,我很好奇它是如何完成的。 最近研究了
我在将两个数组拼接在一起时遇到问题。假设我有两个数组: a = array([1,2,3]) b = array([4,5,6]) 当我执行 vstack((a,b)) 时,我得到了 [[1,2,3]
我拼接 css 文件以提高性能。 现在我想包含一个 minify 函数,如下所示。我该如何合并它? function minify( $css ) { $css = preg_replace(
我有两个 matlab 向量。第一个有 N 个元素,另一个有 k*N。我知道 k 是什么,我想拼接列表,使第一个向量中的每个元素出现在下一个向量中相应的 k 元素之前。例如: k = 3 x = [1
我有以下上下文: https://jsfiddle.net/eqntaqbt/2/ obj.forEach(function(user, index){ var userName = user
假设你有一个像这样的数组: var arr= [{id:121, v:'a'}, {id:232, 'b'}]; 你需要找到 id: 232 并删除它,所以你可以这样做: for (var i = a
似乎是通过 建立报价语法非常低效。例如创建一个整数列表 let q1 = List.foldBack (fun n acc -> ) [1..100000] Real: 00:00:05.714,
我正在为一家希望为其用户创建服务的公司开发应用程序。该公司目前提供时长约为 1.5 小时的视频,他们想要集成的服务是让用户能够“录制”这个较大视频的剪辑,以创建本质上的精彩片段。此服务的最终结果将是
splice 当 OFFSET 在数组中但 LENGTH 的末尾超过数组末尾时,是否可以? 最佳答案 易于尝试。 $ perl -wE' my @a = "a".."e"; my @b =
以前从未使用过 Tkinter,我不太确定如何使用它或它是如何工作的。 Windows IDLE Shell。 import time from tkinter import * input("Pre
这里是 Javascript 新手 -- 我有以下数组: var group = ({ one: value1, two: value2, three:
我的屏幕上打印了以下行: Would you like to pay €xx to POS_ID Latte X 1....€2.50-Salad X 1....€4.00-Wrap X 1..
我是一名优秀的程序员,十分优秀!