You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript获取虚拟DOM(#document)内的信息?

Accessing Content Inside the Iframe's Document

Got it, let's break this down for you. You've already targeted the iframe element correctly—now the key is accessing its internal #document, which is sandboxed inside the iframe element. Here's how to do it:

Step 1: Store the iframe element in a variable (optional but cleaner)

First, let's make your existing selector easier to work with by saving the iframe to a variable:

const targetIframe = document.getElementsByClassName("x-border-panel x-border-layout-ct")[1].getElementsByClassName("x-border-panel")[0];

Step 2: Access the iframe's internal document

To get the #document inside the iframe, use the contentDocument property (supported in modern browsers). For older IE compatibility, you can fall back to contentWindow.document:

const iframeDocument = targetIframe.contentDocument || targetIframe.contentWindow.document;

Step 3: Query elements inside the iframe's document

Now you can interact with the iframe's DOM just like you would with the main page's document. For example, to get the text inside that .listTitle span:

const listTitleText = iframeDocument.querySelector(".listTitle").textContent;
console.log(listTitleText); // Should log "Example"

Important Note: Same-Origin Policy

Keep in mind that this only works if the iframe's content is from the same origin (same domain, protocol, and port) as your parent page. If the iframe loads content from a different origin, browsers will block access to contentDocument due to security restrictions. In that case, you'd need to use something like postMessage to communicate between the parent and iframe if you control both sides.

内容的提问来源于stack exchange,提问作者Ryan Ayton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:14:26