这是评论中链接的我的答案的修改片段。函数返回window
(i) 框架的对象id
通过(id
), or null
,如果(i)frame
没有找到。仅当所有(i)frame
s 位于同一域中。还id
s 给予(i)frame
元素在整个过程中必须是唯一的all涉及窗口结构的文件。
function searchFrame(id) { // id = the id of the wanted (i)frame
var result = null, // Stores the result
search = function (iframes) { // Recursively called function
var n; // General loop counter
for (n = 0; n < iframes.length; n++) { // Iterate through all passed windows in (i)frames
if (iframes[n].frameElement.id === id) { // Check the id of the (i)frame
result = iframes[n]; // If found the wanted id, store the window to result
}
if (!result && iframes[n].frames.length > 0) { // Check if result not found and current window has (i)frames
search(iframes[n].frames); // Call search again, pass the windows in current window
}
}
};
search(window.top.frames); // Start searching from the topmost window
return result; // Returns the wanted window if found, null otherwise
}
这个函数可以找到一个frame
or iframe
与过去的id
无论它放置在窗口结构中的哪个位置。该函数还可以在任何窗口中放置和调用。我会将其放在主页上(作为全局页面)。如果子窗口需要该方法,只需调用top.searchFrame(...)
.
代替id
s, also name
可以使用,只要它们也是唯一的。在这种情况下id
报到searchFrame()
需要编辑为name
check.
对于你的情况,首先你需要给出一个id
到目标iframe
,那么您可以使用如下代码:
var txtClinic = searchFrame('IFRAME_ID').document.getElementById('clinicFlag');
上面的方法对于获取单个引用可能有点大材小用,但是如果您必须在不同窗口中多次获取这些跨窗口引用,那么它非常有帮助。
你的具体任务可以这样完成:
var txtClinic = parent.frames[1].frames[0].document.getElementById('clinicFlag');
name
的(i)frame
也很方便使用frames
收藏。您可以使用名称来代替索引。始终从主页开始链接引用,即从top
。例如:
var iframeWin = top.frames['frame2'].frames['iframe1'];
有用的阅读:
窗口顶部 https://developer.mozilla.org/en-US/docs/Web/API/Window.top
窗口.frameElement https://developer.mozilla.org/en-US/docs/Web/API/Window.frameElement
窗框 https://developer.mozilla.org/en-US/docs/Web/API/Window.frames