对尚未在 DOM 中的元素使用 getElementById 吗?

发布于 2024-07-26 15:57:46 字数 443 浏览 6 评论 0原文

据我所知 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

披肩女神 2024-08-02 15:57:46

如果它不是文档的一部分,则无法使用 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.

﹎☆浅夏丿初晴 2024-08-02 15:57:46

对于 2019 年或之后遇到此问题的任何人,这里有一个更新的答案。

Andrew Noyes 接受的答案是正确的,因为除非该元素存在于文档中并且上面的代码已经包含对所需元素的引用,否则 document.getElementById 将不起作用。

但是,如果您无法以其他方式检索对所需元素的直接引用,请考虑使用 选择器。 选择器允许您通过使用节点与其他节点的关系来检索不一定在 DOM 中的节点,例如:

var child = document.createElement("div");
child.id = "my_id";

var parent = document.createElement("div");
parent.appendChild(child);

var child2 = parent.querySelector("#my_id");

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:

var child = document.createElement("div");
child.id = "my_id";

var parent = document.createElement("div");
parent.appendChild(child);

var child2 = parent.querySelector("#my_id");
白首有我共你 2024-08-02 15:57:46

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.

茶花眉 2024-08-02 15:57:46

如果您创建了它,只需将该对象传递给其他函数并直接访问它?

function createDiv()
{
  var newElement = document.createElement('div');
  doWorkWithDiv(newElement);
}

function doWorkWithDiv(element)
{
  element.className = 'newElementCSS';
  element.innerHTML = 'Text inside newElement';
  addToDoc(element);
}

function addToDoc(element)
{
  document.body.appendChild(element);
}

If you've created it, just pass the object to other functions and access it directly?

function createDiv()
{
  var newElement = document.createElement('div');
  doWorkWithDiv(newElement);
}

function doWorkWithDiv(element)
{
  element.className = 'newElementCSS';
  element.innerHTML = 'Text inside newElement';
  addToDoc(element);
}

function addToDoc(element)
{
  document.body.appendChild(element);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文