Javascript 在 iframe 更改时滚动到父框架的顶部?

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

Scroll to top of parent frame on iframe change?

javascriptdomiframe

提问by spyderman4g63

Is it possible to scroll(0,0) to the top of the parent page when the user browses in an iframe? For example, we have an iframe that is the entire height of the page with search results. When you click the next page button in the iframe, the focus stays at the bottom of the page.

当用户在 iframe 中浏览时,是否可以滚动(0,0)到父页面的顶部?例如,我们有一个 iframe,它是包含搜索结果的页面的整个高度。当您单击 iframe 中的下一页按钮时,焦点将停留在页面底部。

I'm not even sure if it's possible to detect this. The iframe src value doesn't actually change.

我什至不确定是否有可能检测到这一点。iframe src 值实际上并没有改变。

P.S. I don't know why this site must use an iframe, but IE6 is the standard browser so I didn't ask questions.

PS我不知道为什么这个网站必须使用iframe,但是IE6是标准浏览器所以我没有问问题。

回答by spyderman4g63

Actually, the onload event does appear to work when navigating within an iframe.

实际上,在 iframe 中导航时 onload 事件似乎确实有效。

<iframe src="http://mysite.com" height="2392px" width="100%" name="searchFrame" onload="scroll(0,0);"></iframe>

回答by Erwin Wessels

As an alternative, you can have the iframe ask the parent frame to scroll to the top with

作为替代方案,您可以让 iframe 要求父框架滚动到顶部

parent.scrollTo(0,0);

I haven't researched the official browser support for it, but it works in IE9, FF10, Chrome 17 and Safari 5.1.

我没有研究过官方浏览器对它的支持,但它适用于 IE9、FF10、Chrome 17 和 Safari 5.1。

回答by TomGro

<iframe onload="parent.location= 'http://mysite.com/page.html#nameofdivonthetopofpage';" ...