【问题标题】:Jquery - scroll to top on parent page of iframe?Jquery - 在 iframe 的父页面上滚动到顶部?
【发布时间】:2014-03-05 05:27:42
【问题描述】:

我想滚动包含 iframe 的页面。 当 iframe 加载包含 Jquery 代码的页面时,我希望我的父页面滚动到顶部。

这是我现在正在使用的代码,但它不起作用:

<HTML>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
     window.parent.$("body").animate({scrollTop:0}, 'slow');

}
</script>
</head>

我的错误在哪里?为什么当 iframe 页面加载此页面时,父页面不会置顶?

提前谢谢你!

【问题讨论】:

    标签: jquery iframe


    【解决方案1】:

    您可以在父页面中定义函数并从 iframe 调用:
    例如:
    父页面:

    function top(){
        alert('scroll to top')
    }
    

    iframe:

    <button onclick="parent.top()">top</button>
    

    【讨论】:

      【解决方案2】:

      您无法通过这种方式访问​​ iframe 中的父页面。

      如果您可以访问您的父页面,请尝试检查它是否包含 iframe

      $('#iframeID').load(function() {
         $("html, body").animate({scrollTop:0}, 'slow'); // You have to point the animation to your <html> element if you want to scroll your page
      });
      

      【讨论】:

      • 不,我做不到。
      • 试试 $(parent.document).ready(function(){ $(parent.document.firstElementChild).animate({scrollTop:0}, 'slow'); })
      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2010-12-25
      • 2016-09-13
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多