简体   繁体   English

JQuery和框架 - $(文档).ready不起作用

[英]JQuery and frames - $(document).ready doesn't work

I have a page, with some code in js and jQuery and it works very well. 我有一个页面,在js和jQuery中有一些代码,它运行得很好。 But unfortunately, all my site is very very old, and uses frames. 但不幸的是,我的所有网站都非常陈旧,并使用框架。 So when I loaded my page inside a frame, $(document).ready() doesn't fire up. 因此,当我将页面加载到框架内时, $(document).ready()不会启动。

My frameset looks like: 我的框架集看起来像:

<frameset rows="79,*" frameBorder="1" frameSpacing="1" bordercolor="#5996BF" noresize> 
    <frame name="header" src="Operations.aspx?main='Info.aspx'" marginwidth="0" marginheight="0" scrolling="no" noresize frameborder="0">
    <frame name="main" src="Info.aspx" marginwidth="0" marginheight="0" scrolling="auto" noresize frameborder="0">      
</frameset>

My page is loaded into the main frame. 我的页面被加载到main框架中。 What should I do? 我该怎么办?

I have tried the method mentioned in another comment: 我试过另一条评论中提到的方法:

$("#frameName").ready(function() {
    // Write you frame on load javascript code here
} );

and it did not work for me. 它对我不起作用。

this did: 这样做:

$("#frameName").load( function() {
     //code goes here
} );

Even though the event does not fire as quickly - it waits until images and css have loaded also. 即使事件没有快速启动 - 它等待图像和css也加载。

I know this is an old topic. 我知道这是一个古老的话题。 But to help some of you who reach this page, here is my solution: 但是为了帮助一些到达此页面的人,这是我的解决方案:

$($("#frameName")[0].contentWindow.document).ready(function() {
    // Write you frame onready code here
});

If you want to fire the onload event for your frames, then follow these steps: 如果要为帧onload事件,请按照下列步骤操作:

  1. Assign an id and name to each <frame> tag. 为每个<frame>标记分配idname Make sure both id and name attributes value is same. 确保idname属性值相同。

  2. Use the following code to fire the onload event of the frame: 使用以下代码来触发帧的onload事件:

     $("frameName").ready(function() { // Write your frame onload code here } 

I assume this is a similar problem I was having with DOMContentLoaded in an iframe. 我认为这是我在iframe中使用DOMContentLoaded时遇到的类似问题。

I wrote a blog post about it . 我写了一篇关于它的博客文章

The following also worked for me: 以下内容对我也有用:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>
$(window.parent.frames[0].document).ready(function() {
    // Do stuff
});
</script>

The [0] indicates that it is the first frame in the document, [1] would be the second frame, and so on. [0]表示它是文档中的第一帧,[1]是第二帧,依此类推。 This is particularly nice if you do not have control over the mark-up, and it is still utilizing document ready. 如果您无法控制标记,并且仍在使用文档准备就绪,这一点尤其好。

I have worked a long time with this post... here is my solution. 我在这篇文章上工作了很长时间......这是我的解决方案。

test.html 的test.html

<!DOCTYPE HTML>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>

    <script>        
      document.write('<frameset><frame name="frame_content" id="frame_content"></frame></frameset>');

      $('#frame_content').attr('src', 'test2.html');
      $('#frame_content').load(function()
      {
        if('${"#header"}' != '') {
          $("#header", frame_content.document).remove();
        }
      });
      if($('#frame_content').complete) $('#frame_content').trigger("load");
    </script>

</head>
</html>

test2.html test2.html

<!DOCTYPE HTML>
<html>

  <head>
  </head>

  <body>
    <div id="header">You will never see me, cause I have been removed!</div>
  </body>
</html>

您是否尝试将jQuery代码放在Info.aspx页面中?

我不知道它是否是最好的解决方案,但是当我删除$(document).ready()并保留它的正文时,一切都很完美。

Not sure what you're trying to do, but I have an even older classic asp app that operates out of frames, and I just recently added jQuery functionality and it is working great. 不知道你想要做什么,但我有一个更老的经典asp应用程序,它运行框架,我刚刚添加了jQuery功能,它工作得很好。 The $(document).ready() works fine within a frame, but if you wish to reference the DOM in another frame, you'll have to use the Frame's onload event to let you know when the frame's DOM is loaded. $(document).ready()在一个框架内工作正常,但如果你想在另一个框架中引用DOM,你必须使用Frame的onload事件让你知道何时加载了框架的DOM。 Admittedly, I used iFrames, but the concept should be the same. 不可否认,我使用的是iFrame,但概念应该是相同的。

No need to modify the markup. 无需修改标记。 Just fix the selector. 只需修复选择器。 It should be: 它应该是:

$("frame[name='main']").ready(function(){..}); 

not

$("#frameName").ready(function(){..}); 

Note: it seems the jQuery ready event fires multiple times. 注意:似乎jQuery ready事件多次触发。 Make sure that is OK with your logic. 确保你的逻辑没问题。

This answer may be late, but this reply may help someone like me... 这个答案可能会迟到,但这个回复可能会对像我这样的人有所帮助......

This can be done via native Javascript code - 这可以通过原生Javascript代码完成 -

ifrm2 = var ifrm2 = document.getElementById('frm2');
if (ifrm2.contentDocument.readyState == 'complete') {
  //here goes the code after frame fully loaded
}

 //id = frm2 is the id of iframe in my page

There is no reason for $(document).ready() not to be called. 没有理由不调用$(document).ready() Be sure your page contains an include to jquery.js . 确保您的页面包含jquery.js包含。 Try to do a simple test with an empty HTML page and just an alert to see if there is another problem. 尝试使用空的HTML页面进行简单的测试,只需一个警报即可查看是否存在其他问题。

If you are trying to use this inside the HTML page that contains the frame's definition, keep in mind that there is no document there, you will have to use the 如果你试图在包含框架定义的HTML页面中使用它,请记住那里没有文档,你将不得不使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM