如何访问JS中新创建的DOM元素? (我已经使用了附录)
我正在做一个待办事项清单。每个列表项目都在创建时包含一个删除按钮。
但是我无法在脚本中访问这些删除按钮,因为尽管我使用了“ .appendchild”,但它不包含在DOM中。谁能帮忙?
const buttonSubmit = document.querySelector('#button-submit');
const form = document.querySelector('form');
const icons = document.querySelector('#icons');
let toDoList = document.querySelector('#todolist');
const input = document.querySelector('#formtext');
form.addEventListener('submit', (e) => {
e.preventDefault();
const newListItem = document.createElement('li');
newListItem.innerHTML = '<span>' + input.value + '</span>' +
'<span id="icons">' +
'<button id="check" class="buttonlist">' + '<img src="checked.png" alt="">' + '</button>' +
'<button id="remove" class="buttonlist">' + '<img src="remove.png" alt="">' + '</button>' +
'<button id="edit" class="buttonlist">' + '<img src="edit.png" alt="">' + '</button>' + '</span>';
toDoList.appendChild(newListItem);
form.reset();
})
const buttonCheck = document.querySelector('#check');
const buttonEdit = document.querySelector('#edit');
const buttonRemove = document.getElementById('remove');
buttonRemove.addEventListener('click', function() {
alert('remove list item');
})
<div class="container-box">
<h1>To Do List</h1>
<br>
<form action="">
<input type="text" id="formtext" name="formtext">
<button id="button-submit">Add Item</button>
</form>
<br><br><br><br>
<!-- Dynamic list here -->
<ul id="todolist"> </ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题
ID必须是唯一的,每次您将任务添加到列表中时 - 第一个后,一切都是无效的HTML。当针对ID时,浏览器会找到第一个ID,然后停止并忽略重复ID。为任何重复的标签使用类和/或名称属性。
删除按钮不起作用的原因是因为在不存在时定义了对按钮的引用。
此外
/strong>仅适用于第一个按钮。
图II
解决方案
参考标签 是创建它们的。在OP( o igninal p ost)中,
&lt; li&gt;
的内容是呈现的,这使得新创建的标签有问题加上htmlstring如果在大多数情况下是可行且实用的,则应避免与动态创建的标签结合。要处理未知数量的动态创建标签的事件,请将事件绑定到静态祖先标签,在OP中,该标签是
&lt; ul&gt;
或任何其父母标签(甚至&lt; Body&gt;
,文档
和窗口
,但最好尽可能接近)。然后将其进行使事件处理程序控制哪个标签响应以及如何响应。有关更多详细信息,请参见附录。有两个示例:
示例a - 修订的OP代码
示例B - 使用 htmlformelement 接口,请参见 appendix
两个示例都评论了逐步的详细信息
示例
示例b
附录
evest
/DEVELECTER.MOZILLA.org/en-us/docs/learn/javascript/building_blocks/events#event_delegation“ rel =“ nofollow noreferrer”> event develegation
htmlformelement
Problems
ids must be unique, every time you add a task to the list -- after the first one everything is invalid HTML. When directed to an id the browser will find the first id then stop and ignore the duplicate ids. Use class and/or name attributes for any replicated tags.
The reason why the remove button doesn't work is because the reference to the button was defined when it didn't exist.
Figure I
Moreover, even if that was fixed by referencing the button after it was created, binding it as shown on Figure II will only work for the first button only.
Figure II
Solution
Reference tags after they are created. In the OP (Original Post), the contents of the
<li>
is rendered htmlString which makes referencing newly created tags problematic plus binding to dynamically created tags individually should be avoided if it's feasible and practical in which in most cases it is.To handle events for an unknown amount of dynamically created tags, bind the event to a static ancestor tag, which in the OP is
<ul>
or any of it's parent tags (even<body>
,document
, andwindow
but it's best to be as close as possible). Then make it so the event handler controls which tags respond and how. See Appendix located at the very end of this answer for more details.There are two examples:
Example A - revised OP code
Example B - a todo list using HTMLFormElement interface, see Appendix
Both examples have commented step-by-step details
Example A
Example B
Appendix
Events
Event delegation
HTMLFormElement
HTMLFormControlsCollection
Form Controls