JavaScript - 在 iframe 中访问 iframe

JavaScript - access iframe in iframes

要访问我的主文档中的 iframe,我使用以下代码

// First iFrame in the main document
var iFrame = document.querySelector("iframe");

// Second iFrame in the iFrame
var iFrame_2 = iFrame.contentWindow.document.querySelector("iframe");

// Third iFrame ... here i get a "permission denied error"
var iFrame_3 = iFrame_2.contentWindow.document.querySelector("iframe");

当我尝试访问第三个 iFrame 时,我得到拒绝访问文档的权限,为什么?

第二个 iframe 是否链接到外部网站? 他们的服务器可能会阻止 CORS(跨源资源共享),这就是为什么您的第三个 iframe 被拒绝的原因。