强制网页在页面顶部加载

3
我有一个包含一些外部源代码加载的IFrame的html页面。我无法控制这些外部资源。
当网页加载时,它会聚焦于其中一个IFrame而不是页面顶部。
以下是我的简单HTML页面:
<html>
   <head>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
      <div id="topp" tabindex="0"></div>
   </head>
   <div id="topp" tabindex="0"></div>
   <body>
      <p id="test">test 1 2 3 4.</p>
      <br><br>
      <h2>IFrame 1</h2>
      <iframe src="https://..../index1.html" width="100%" height="825px;"></iframe>
      <iframe src="https://..../index2.html" width="100%" height="825px;"></iframe>
      <iframe src="https://..../index3.html" width="100%" height="825px;"></iframe>
      <iframe src="https://..../index4.html" width="100%" height="825px;"></iframe>
   </body>
</html>

本文关注的是Iframe index3.html,因此我尝试通过在页面正文中包含一些javascript来创建解决方法。这样,随着DOM的渲染,我就可以强制页面滚动回顶部。但是它没有起作用。

以下是我尝试过的方法:

$(document).ready(function(){
    $(this).scrollTop(0);
});

有没有一种方法可以查找IFrame是否已经加载所有的SRC,然后将焦点聚焦在页面顶部?


不知道 <iframe> 中的脚本,要想找出问题并进行防范是相当困难的。你能检查一下那个 iframe 的内容并分享给我们吗? - FZs
页面的HTML非常大,但我发现<body onload="onBodyLoad()">会运行一些JS代码,并且还会加载一些动态HTML代码,你需要找到具体的内容,以便我可以为您查找。 - Nick Kahn
1个回答

1
您可以使用以下代码替换您的JavaScript代码:

window.scrollTo(0, 0);

那行代码将在加载后强制页面滚动到顶部。
您可以使用以下代码添加平滑滚动:
window.scroll({
  top: 0, 
  left: 0, 
  behavior: 'smooth',
});

我希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接