简体   繁体   English

document.getElementById 不检索实际存在的元素

[英]document.getElementById doesn't retrieve an element that actually exists

Here is the DOM that clearly contains an input with id= RESULT_TextField-11 :这是 DOM 明确包含一个 id= RESULT_TextField-11的输入: 在此处输入图片说明

But when I try to retrieve it from Chrome console using document.getElementById method, it doesn't work:但是当我尝试使用 document.getElementById 方法从 Chrome 控制台检索它时,它不起作用:

在此处输入图片说明

document.getElementsByName doesn't work either. document.getElementsByName 也不起作用。

Why could this happen?为什么会发生这种情况? The first screenshot was taken before the second one.第一个屏幕截图是在第二个屏幕截图之前拍摄的。

What's interesting is that when I try to run the same code in console a few minutes later, it often works.有趣的是,当我尝试在几分钟后在控制台中运行相同的代码时,它通常有效。

Some information that may help:一些可能有帮助的信息:

The input can't be found because it's within an iFrame.无法找到输入,因为它在 iFrame 内。 You would have to target it with something like this instead:你必须用这样的东西来定位它:

document.getElementById('frame-one997541297').contentWindow.document.getElementById('RESULT_TextField-11')

However, that won't work either because the iFrame is loaded from another domain.但是,这也不起作用,因为 iFrame 是从另一个域加载的。 So scripts at your site won't be able to interact with content from the iFrame's site due to the same origin policy .因此,由于同源策略,您站​​点上的脚本将无法与来自 iFrame 站点的内容进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM