对尚未在 DOM 中的元素使用 getElementById 吗?
据我所知 document.getElementById('myId') 只会查找文档中已有的 HTML 元素。 假设我已经通过 JS 创建了一个新元素,但尚未将其附加到文档正文中,是否有一种方法可以通过其 id 访问该元素,就像我通常使用 getElementById 所做的那样?
var newElement = document.createElement('div');
newElement.id = 'myId';
// Without doing: document.body.appendChild(newElement);
var elmt = document.getElementById('myId'); // won't work
有解决方法吗? (我必须告诉你,我不想存储对此特定元素的任何引用,这就是为什么我需要通过其 Id 访问它)
谢谢!
As far as I know document.getElementById('myId') will only look for HTML elements that are already in the document. Let's say I've created a new element via JS, but that I haven't appended it yet to the document body, is there's a way I can access this element by its id like I would normally do with getElementById?
var newElement = document.createElement('div');
newElement.id = 'myId';
// Without doing: document.body.appendChild(newElement);
var elmt = document.getElementById('myId'); // won't work
Is there a workaround for that?
(I must tell that I don't want to store any reference to this particular element, that's why I need to access it via its Id)
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它不是文档的一部分,则无法使用
document.getElementById
获取它。getElementById
执行 DOM 查找,因此该元素必须位于树中才能找到。 如果您创建浮动 DOM 元素,它仅存在于内存中,并且无法从 DOM 访问。 它必须添加到 DOM 中才能可见。如果稍后需要引用该元素,只需将引用传递给另一个函数即可 - JavaScript 中的所有对象都是通过引用传递的,因此从另一个函数中处理该浮动 DOM 元素会修改原始元素,而不是副本。
If it isn't part of the document, then you can't grab it using
document.getElementById
.getElementById
does a DOM lookup, so the element must be in the tree to be found. If you create a floating DOM element, it merely exists in memory, and isn't accessible from the DOM. It has to be added to the DOM to be visible.If you need to reference the element later, simply pass the reference to another function--all objects in JavaScript are passed by reference, so working on that floating DOM element from within another function modifies the original, not a copy.
对于 2019 年或之后遇到此问题的任何人,这里有一个更新的答案。
Andrew Noyes 接受的答案是正确的,因为除非该元素存在于文档中并且上面的代码已经包含对所需元素的引用,否则
document.getElementById
将不起作用。但是,如果您无法以其他方式检索对所需元素的直接引用,请考虑使用 选择器。 选择器允许您通过使用节点与其他节点的关系来检索不一定在 DOM 中的节点,例如:
For anyone stumbling upon this issue in or after 2019, here is an updated answer.
The accepted answer from Andrew Noyes is correct in that
document.getElementById
won't work unless the element exists in the document and the above code already contains a reference to the desired element anyway.However, if you can't otherwise retrieve a direct reference to your desired element, consider using selectors. Selectors allow you to retrieve nodes that aren't necessarily in the DOM by using their relationship to other nodes, for example:
getElementById 是文档对象的方法。 它不会返回文档中未包含的任何内容。
关于不存储参考,是吗? 如果你可以通过 id 神奇地将它从空气中拉出来,那么空气将是对它的引用。
getElementById is a method on the document object. It's not going to return anything not in the document.
On not storing a reference, huh? If you could magically pull it out of the air by id, then the air would be a reference to it.
如果您创建了它,只需将该对象传递给其他函数并直接访问它?
If you've created it, just pass the object to other functions and access it directly?