javascript 从其他框架集中的框架调用javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20098533/
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 17:42:19  来源:igfitidea点击:

Call javascript function from frame in other frameset

javascripthtmlframes

提问by seph

My site is setup like this:

我的网站是这样设置的:

<frameset rows="80,*">
    <frame name="top" id="top" src="header.html">
    <frameset id="innerframe" cols="300,*">
        <frame name="nav" src="nav.html">
    </frameset>
</frameset>


In header.htmlI have:


header.html我有:

function fAlert() {
    alert('test');
}

How can I call fAlert()in nav.html?

I tried

我怎样才能打fAlert()进来nav.html

我试过

var fframe = parent.document.getElementById('top');
fframe.fAlert();

and also

并且

parent.frames.top.fAlert();

parent.frames.top.fAlert();

but it didnt work (fAlert is undefined).

Any ideas how I can accomplish this?

但它没有用(fAlert is undefined)。

我有什么想法可以做到这一点吗?

回答by user1329482

First off, don't use framesets and frames. Use iframes--frames are deprecated.

首先,不要使用框架集和框架。使用 iframes——不推荐使用框架。

Secondly, provide an id for the iframe (or frame, if you must) in order to direct the function call correctly. (You've already pretty much done this, but I'm being methodical.) I wouldn't name it 'top' because 'top' already has a meaning in terms of windows and frames.

其次,为 iframe(或框架,如果必须)提供 id,以便正确引导函数调用。(你已经做了很多这件事,但我是有条不紊的。)我不会将它命名为“顶部”,因为“顶部”已经在窗口和框架方面具有意义。

From inside the nav frame, parent.insertYourFrameIdHere.fAlert() should work correctly. This assumes two things: 1) The page and the frame contents come from the same domain, and 2) header.html loaded correctly and there were no script errors in it. Script errors or other issues could keep the function from ever being created.

从导航框架内部,parent.insertYourFrameIdHere.fAlert() 应该可以正常工作。这假设两件事:1) 页面和框架内容来自同一个域,2) header.html 正确加载并且其中没有脚本错误。脚本错误或其他问题可能会阻止函数的创建。

回答by user3005791

For the html in your question following should work.

对于您的问题中的 html,以下应该有效。

window.parent.parent.frames[0].fAlert();