javascript jQuery:我可以将函数绑定到父窗口的 onscroll 事件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5491266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:22:33  来源:igfitidea点击:

jQuery: Can I bind a function to the onscroll event of the parent window?

javascriptjqueryeventsiframejavascript-events

提问by Bungle

I have a page that does not load jQuery. An <iframe>included on that page (in the same domain) does load jQuery.

我有一个不加载 jQuery 的页面。一个<iframe>包含网页(在同一个域)在不加载的jQuery。

Using jQuery, is it possible to bind a function to an event of the windowobject of the parent page, even though it doesn't load jQuery?

使用 jQuery,是否可以将函数绑定到window父页面对象的事件,即使它没有加载 jQuery?

Accessing the parent windowdoesn't seem to be a problem; this works (at least in Firefox 4):

访问父对象window似乎不是问题;这有效(至少在 Firefox 4 中):

console.log($(parent)); // displays "jQuery(Window index.html)" in the console

To give some context, I'm developing the document to be loaded within the <iframe>, but I don't control the parent page, so I can't use a solution that requires adding jQuery to it. Ultimately I want to subscribe to the onscrollevent of the parent windowto execute a function in the <iframe>every time that scrolling happens in the viewport.

为了提供一些上下文,我正在开发要在 .js 中加载的文档<iframe>,但我不控制父页面,因此我无法使用需要向其中添加 jQuery 的解决方案。最终,我想订阅onscroll父事件window以在<iframe>每次在视口中发生滚动时执行一个函数。

I've tried (within the <iframe>):

我试过(在 内<iframe>):

function my_function() {
  console.log('Scrolling...');
}
$(parent).scroll(my_function);

However, that didn't work.

然而,这并没有奏效。

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!



EDIT:

编辑:

Here is a working example:

这是一个工作示例:

http://troy.onespot.com/static/stack_overflow/onscroll/parent.html

http://troy.onespot.com/static/stack_overflow/onscroll/parent.html

The document in the <iframe>is here:

中的文件在<iframe>这里:

http://troy.onespot.com/static/stack_overflow/onscroll/iframe.html

http://troy.onespot.com/static/stack_overflow/onscroll/iframe.html

回答by ?ime Vidas

Try:

尝试:

parent.onscroll = function() { ... };


Update:

更新:

Try this, it works for me:

试试这个,它对我有用:

$(parent.document).scroll(function() { ... });

回答by Amr Elgarhy

If you are trying to control a parent page which in another domain, so this is not possible for security reasons.

如果您试图控制另一个域中的父页面,那么出于安全原因,这是不可能的。

And you can find many articlesabout this problem, If this is not your case tell us because sure there is another problem.

你可以找到很多关于这个问题的文章,如果这不是你的情况,请告诉我们,因为肯定还有另一个问题。

If in the same domain, what about using this:

如果在同一个域中,那么使用这个怎么样:

$(document).ready(function(){ $(this).parent().scroll(function(){//stuff}); });

$(document).ready(function(){ $(this).parent().scroll(function(){//stuff}); });

$(document).ready(function(){ 
   $(window.parent.document).scroll(function(){//stuff});
});