【问题标题】:iframe to automatically scroll to bottomiframe 自动滚动到底部
【发布时间】:2012-09-30 03:53:48
【问题描述】:

我无法让这段代码工作,当 iframe 刷新时,iframe 应该会自动向下滚动到底部。

FIDDLE

代码:

<html>
<!--
  Created using jsbin.com
  Source can be edited via http://jsbin.com/ujuci5/2/edit
-->
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
  <p>iframe:</p>
  <iframe src="http://jsbin.com/aloko5" id="frame" frameborder="0" width="200px" height="300px"></iframe>
<script>
$(window).load(function ()
{
  var $contents = $('#frame').contents();
  $contents.scrollTop($contents.height());
});
</script>

</body>
</html>​

有人帮我解决这个问题,我有点卡住了

【问题讨论】:

  • 你的小提琴说它应该使用 MooTools。但是您的代码调用了 jQuery。你的小提琴设置错了吗?
  • 您无法修改框架:不安全的 JavaScript 尝试从 URL 为 fiddle.jshell.net/_display 的框架访问 URL 为 jsbin.com/aloko5 的框架。域、协议和端口必须匹配。
  • 我在我的桌面上试过了,即使它是一个安全的 url 仍然没有向下滚动

标签: javascript scrollbar


【解决方案1】:

您可以访问 iframe 中的代码吗?我相信您需要在 iframe 代码本身中设置 scrollTop。

例如:

$("body").scrollTop(100);

编辑:在这里找到更多信息:How to get scrollTop of an iframe

【讨论】:

    【解决方案2】:

    要拦截的 onLoad 事件应该是来自 iframe 本身的事件,而不是来自窗口的事件。

    另外,来自 Blender 的评论部分有效。

    仅当在 iframe 中打开的文档与父文档属于同一域时,此设置才有效。并且使用相同的协议:

    - http://domain/main.aspx as the container and http://domain/inner.aspx at the iframe is valid
    - http://domain/main.aspx as the container and https://domain/inner.aspx at the iframe will be blocked
    - http://domain/main.aspx as the container and http://another/inner.aspx at the frame will also be blocked
    

    (从我的 PDA 发送)

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 2010-12-25
      • 2016-09-15
      • 2018-08-21
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多