如何使用jQuery滚动/动画iframe.工作在FF, IE, Safari,但不是Chrome



我的简单例子:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Iframe Scrolling Test</title>
        <script type="text/javascript" language="javascript" src="jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            function scroll() {
                var iframeDocx = $("#tpTopicContentIframe");
                iframeDocx.contents().find("html,body").animate({ scrollTop: 1000 }, 1000);
            };
        </script>
    </head>
    <body>
        <a href="#" onclick="scroll();">Scroll to 1000px</a>
        <div id="tpTopicContentIframeContainer">
            <iframe frameborder="0" id="tpTopicContentIframe" src="docpart.html" style="height: 400px; width: 600px;"></iframe>
        </div>
    </body>
</html>

在Chrome中,我得到这个错误:

JavaScript尝试使用URL文件:///C:/Users/DENIS/Projects/Tests/Iframe%20Scrollto/docpart.html从URL文件://C:/Users/DENIS/Projects/Tests/Iframe%20Scrollto/index.html访问框架#。域、协议和端口必须匹配。

但它是同一个定义域。它适用于FF, IE, Safari,但不支持Chrome。

为什么Chrome这么严格?如何在所有浏览器中实现此功能?

浏览器还不能正确处理file://协议。只要页面来自同一域,如果您通过http访问页面,您的代码就可以正常工作。

最新更新