Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在HTMLCollection'下访问子元素;s_Javascript - Fatal编程技术网

Javascript 在HTMLCollection'下访问子元素;s

Javascript 在HTMLCollection'下访问子元素;s,javascript,Javascript,如何访问HTMLCollection的子元素 我的代码在以下位置返回:console.log(iframeContent)。我想接触下面突出显示的孩子,并对他们进行循环/询问 完整代码如下: function getPageStructure() { const iframe = document.getElementById('builder-showcase'); const iframeDocument = iframe.contentDocument || iframe.con

如何访问HTMLCollection的子元素

我的代码在以下位置返回:
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不是一个函数
是否记录了您试图针对的整个数组?