【发布时间】:2017-08-13 01:05:55
【问题描述】:
我在页面中嵌入了一个 iframe。它们都在不同的域上,但我可以访问两者。
当点击 iframe 中的链接时,我想滚动到父窗口的顶部。是这样的:
在 iframe 页面内。
window.parent.ScrollToTop(); // Scroll to top function
在父页面上:
window.ScrollToTop = function(){
$('html,body', window.document).animate({
scrollTop: '0px'
}, 'fast');
};
这可能吗?
【问题讨论】:
-
同源策略阻止你访问windows跨域调用任何此类方法。这样做的方法是使用
postMessage让 iframe 向父级发送消息,然后父级可以自行滚动。
标签: javascript jquery html iframe cross-domain