Javascript,从 iFrame 访问框架集中的嵌套框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17581928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:52:04  来源:igfitidea点击:

Javascript, accessing nested frames in framesets from a iFrame

javascripthtmliframeframe

提问by ThunderHorse

I'm having issues accessing a frame that is part of a page loaded into a iframe. The frame is nested in multiple framesets.

我在访问作为加载到 iframe 的页面的一部分的框架时遇到问题。框架嵌套在多个框架集中。

My html looks like this:

我的 html 看起来像这样:

<iframe name="sourceframe" src="siteindex.html">
    #document <-- this appears in the dom tree. Not sure if it's relevant.
        <html>
            <head></head>
            <frameset>
                ...other frames
                <frameset>
                    <frame name="targetframe" src="sitesubpage.html">
                        ...I want to access this frame and replace the contents with a different html page...
                    </frame>
                </frameset>
            </frameset>
        </html>
</iframe>

I have read similar questions none of which have a solution that I can get to work.

我读过类似的问题,但没有一个可以解决我可以开始工作的问题。

Any suggestions would be greatly appreciated.

任何建议将不胜感激。

回答by Teemu

window.frames['sourceframe'].frames['targetframe'].location.href = 'new_html_page';

FF and Chrome have framescollection only in window, IE and Opera have it also in the document. The above code works (tested), when you get your timing corrected.

FF 和Chrome 中只有frames收藏window,IE 和Opera 中也有document。当您纠正时间时,上述代码有效(已测试)。

Try to use some timeout before changing the the href, or trigger the change from the #targetframe. Also you can attach an onloadevent listener to the iframe, and trigger the change in it. One more, you can place the script creating the iframejust before closing bodytag, then you can use window.onloadto change the page in the depths...

尝试在更改 之前使用一些超时href,或从#targetframe. 您也可以将onload事件侦听器附加到iframe,并触发其中的更改。再一个,您可以将创建iframe关闭body标签之前的脚本放置,然后您可以使用window.onload更改深度的页面......

回答by StackSlave

You could give your frameelement with name='targetframe'an id='targetframe'and do something like:

你可以framename='targetframe'an给你的元素id='targetframe'并做一些类似的事情:

function E(e){
  return document.getElementById(e);
}
function getChildFrame(id){
  var t = E(id);
  var f = t.contentWindow || t.contentDocument;
  if(f.document)f = f.document;
  return f;
}
var tf = getChildFrame('targetframe');
tf.style.backgroundColor = '#007';

See

http://www.w3schools.com/jsref/prop_frame_contentwindow.asp

http://www.w3schools.com/jsref/prop_frame_contentwindow.asp

and

http://www.w3schools.com/jsref/prop_win_frames.asp

http://www.w3schools.com/jsref/prop_win_frames.asp

for more details. Of course, the Same Origin Policy applies. In other words you can't access someone else's frame, without permission.

更多细节。当然,同源政策适用。换句话说frame,未经许可,您无法访问其他人的。

By the way, you can access the framesarray with a number as well.

顺便说一下,您也可以使用frames数字访问数组。