Javascript JQuery $('iframe').ready 为什么不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7899737/
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-08-24 03:59:40  来源:igfitidea点击:

JQuery $('iframe').ready why doesn't work?

javascriptjqueryhtmliframe

提问by Lork

I have a very long iframe inside my parent page. When you reload or click on a link inside i frame's page it is loaded inside it but the purent window must be scroll up.

我的父页面中有一个很长的 iframe。当您重新加载或单击 i 框架页面内的链接时,它会加载到其中,但纯窗口必须向上滚动。

I have tried variuos code example:jquery which event is better thatn thisand How to scroll parent page with iFrame reloadParent page body code:

我尝试过各种代码示例:jquery 哪个事件更好,以及 如何使用 iFrame 重新加载父页面正文代码滚动父页面

<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
    $('iframe').load(function(){
         $(window).scrollTop(0);
    });
});</script>
<iframe frameborder="0" height="1000" id="iframe" src="http://mysite.com" width="800"></iframe>

And now this is my final code in parent page: (that doesn't work)

现在这是我在父页面中的最终代码:(不起作用)

<script type="text/javascript">
$('iframe').ready(function(){
     $(window).scrollTop(0);
});
</script>
<iframe frameborder="0" height="1000" id="iframe" align="top" src="http://foicam.altervista.org/listadinamica.php" width="800" ></iframe>

but the problem is that each code scroll up the parent page AFTER the frame is compleately loaded (images include). Whant i want is that the parent page scrolls up BEFORE iframe has finished to load.

但问题是,在框架完全加载(包括图像)后,每个代码都会向上滚动父页面。我想要的是父页面在 iframe 加载完成之前向上滚动

if you click on last image or scroll down the page and then reload only iframe you'll see what i mean!

如果您单击最后一张图片或向下滚动页面,然后仅重新加载 iframe,您就会明白我的意思!

Thanx a lot, and sorry but I discovered the existence of this language iesterday afternoon!!!

非常感谢,对不起,我昨天下午发现了这种语言的存在!!!

回答by Jules

Change your code to:

将您的代码更改为:

<iframe frameborder="0" height="1000" id="iframe" align="top" src="http://foicam.altervista.org/listadinamica.php" width="800" ></iframe>

<script type="text/javascript">
$('iframe').ready(function(){
     $(window).scrollTop(0);
});
</script>

Your script is being called before your iframe is part of your DOM. If you put your script after your iframe is part of your DOM, it will recognize it and $('iframe')will actually find the object.

您的脚本在 iframe 成为 DOM 的一部分之前被调用。如果您将脚本放在 iframe 是 DOM 的一部分之后,它会识别它并$('iframe')实际找到该对象。

回答by Quentin

The script appears before the iframe in the source, and doesn't have anything to delay its execution (such as being called in onready).

该脚本出现在源代码中的 iframe 之前,并且没有任何延迟其执行的内容(例如在 中调用onready)。

Consequently, when it executes, the iframe has not been added to the DOM, and the selector doesn't find the frame. The event handler will only be bound to any iframes that appear earlierin the source.

因此,当它执行时,iframe 还没有被添加到 DOM 中,并且选择器找不到该框架。事件处理程序只会绑定到源中较早出现的任何 iframe 。