Javascript 在HTMLCollection'下访问子元素;s
如何访问HTMLCollection的子元素 我的代码在以下位置返回:Javascript 在HTMLCollection'下访问子元素;s,javascript,Javascript,如何访问HTMLCollection的子元素 我的代码在以下位置返回:console.log(iframeContent)。我想接触下面突出显示的孩子,并对他们进行循环/询问 完整代码如下: function getPageStructure() { const iframe = document.getElementById('builder-showcase'); const iframeDocument = iframe.contentDocument || iframe.con
console.log(iframeContent)代码>。我想接触下面突出显示的孩子,并对他们进行循环/询问
完整代码如下:
function getPageStructure() {
const iframe = document.getElementById('builder-showcase');
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
const iframeContent = iframeDocument.getElementsByTagName('body')[0].children;
console.log(iframeContent);
}
已尝试以下方法:
console.log(iframeContent.children);
console.log(iframeContent[0].children);
但在这两个服务器上都会收到未定义的错误消息
const iframeContent = iframeDocument.getElementsByTagName('body')[0].children;
已经抓取了.children
,所以记录console.log(iframeContent.children)代码>不起作用
改用console.log(iframeContent)
如果console.log(iframeContent)
获取包含子元素的数组,则可以使用console.log(iframeContent[0])
来选择iframeContent
中的div
元素
您还可以使用iframeContent.forEach(child=>console.log(child))
来记录iframeContent
的所有子元素(这些不是HTMLCollection的子元素,它们是集合中第一个元素的子元素,这是您在0
键下看到的。)@当我尝试console.log(iframeContent.children)时,我得到了undefined
代码>您是否尝试过@04FS的建议iframeContent[0]。children
Yep,我在发布问题之前已经试过了。我得到:未捕获类型错误:无法读取未定义的属性“children”
谢谢。这是可行的,但我如何访问子级的子级呢?对于iframeContent.forEach(child=>console.log(child))
,我会得到错误消息TypeError:iframeContent.forEach不是一个函数是否记录了您试图针对的整个数组?