javascript - Gain access to child elements under HTMLCollection's
问题描述
How do you gain access to child elements under HTMLCollection's children?
My code returns the following at: console.log(iframeContent);
. I want to gain access to the children highlighted below and loop through/interrogate them.
Full code below:
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);
}
Have tried the following:
console.log(iframeContent.children);
console.log(iframeContent[0].children);
but getting an undefined error message on both.
解决方案
const iframeContent = iframeDocument.getElementsByTagName('body')[0].children;
已经抓住了.children
,所以日志记录console.log(iframeContent.children);
不起作用。
改为使用console.log(iframeContent)
。
如果console.log(iframeContent)
获取包含子项的数组,则可以使用console.log(iframeContent[0])
,反之亦然,以选择其中的div
元素iframeContent
。
您还可以使用iframeContent.forEach(child => console.log(child))
记录iframeContent
.
推荐阅读
- python - 如何使用字典创建数据框并设置索引?
- javascript - Bootstrap Modal 中的 Codemirror 编辑器在单击之前不会加载内容
- css - 在 Bootstrap 4 中编写自定义实用程序类
- swift - 如何在 SwiftUI 中调整 SF Symbol 按钮的大小?
- firebase - 用户注册firebase的颤振问题
- java - 遍历二维数组
- javascript - Sequelize - 使用 es6 和模块运行迁移
- python - .get() 在空的 Tkinter 条目小部件上返回什么
- php - 如何在 GitHub 存储库中包含 Composer
- flutter - 悬停时显示/隐藏下拉菜单 Flutter for web