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

发布于 2024-07-08 15:50:33 字数 609 浏览 9 评论 0原文

我有一个页面,其中有一些 js 和 jQuery 代码,它运行得很好。 但不幸的是,我的所有网站都非常非常旧,并且使用框架。 因此,当我将页面加载到框架内时, $(document).ready() 不会启动。

我的框架集如下所示:

<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>

我的页面已加载到 main 框架中。 我应该怎么办?

I have a page, with some code in js and jQuery and it works very well. 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.

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. What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

江南月 2024-07-15 15:50:33

我已经尝试过另一条评论中提到的方法:

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

但它对我不起作用。

这样做是这样的:

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

尽管事件没有那么快触发 - 它会等到图像和 CSS 也加载完毕。

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.

憧憬巴黎街头的黎明 2024-07-15 15:50:33

我知道这是一个老话题了。 但为了帮助一些到达此页面的人,这是我的解决方案:

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

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
});
方觉久 2024-07-15 15:50:33

我认为这与我在 iframe 中使用 DOMContentLoaded 时遇到的类似问题。

我写了一篇关于它的博客文章

I assume this is a similar problem I was having with DOMContentLoaded in an iframe.

I wrote a blog post about it.

乱世争霸 2024-07-15 15:50:33

如果您想为框架触发 onload 事件,请按照以下步骤操作:

  1. 为每个 分配一个 idname > 标签。 确保 idname 属性值相同。

  2. 使用以下代码触发框架的onload事件:

    $("frameName").ready(function() {  
        // 在这里写下你的框架加载代码 
      } 
      

If you want to fire the onload event for your frames, then follow these steps:

  1. Assign an id and name to each <frame> tag. Make sure both id and name attributes value is same.

  2. Use the following code to fire the onload event of the frame:

    $("frameName").ready(function() { 
      // Write your frame onload code here
    }
    
挖鼻大婶 2024-07-15 15:50:33

以下内容也对我有用:

<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>

[0] 表示它是文档中的第一帧,[1] 表示第二帧,依此类推。 如果您无法控制标记,并且它仍在使用文档就绪,那么这尤其好。

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. This is particularly nice if you do not have control over the mark-up, and it is still utilizing document ready.

妳是的陽光 2024-07-15 15:50:33

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

Have you tried to put the jQuery code inside the Info.aspx page?

所有深爱都是秘密 2024-07-15 15:50:33

不知道你想做什么,但我有一个更旧的经典 asp 应用程序,可以在框架外运行,而且我最近添加了 jQuery 功能,它运行得很好。 $(document).ready() 在一个框架内工作正常,但如果您希望在另一个框架中引用 DOM,则必须使用框架的 onload 事件来让您知道框架的 DOM 何时加载。 诚然,我使用了 iFrame,但概念应该是相同的。

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. 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. Admittedly, I used iFrames, but the concept should be the same.

李不 2024-07-15 15:50:33

我在这篇文章上工作了很长时间......这是我的解决方案。

测试.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>

测试2.html

<!DOCTYPE HTML>
<html>

  <head>
  </head>

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

I have worked a long time with this post... here is my solution.

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

<!DOCTYPE HTML>
<html>

  <head>
  </head>

  <body>
    <div id="header">You will never see me, cause I have been removed!</div>
  </body>
</html>
帝王念 2024-07-15 15:50:33

我不知道这是否是最好的解决方案,但是当我删除 $(document).ready() 并保留其主体时,一切都完美运行。

I don't know if it is the best solution, but when I remove $(document).ready() and keep its body, everything works perfectly.

装迷糊 2024-07-15 15:50:33

无需修改标记。 只需修复选择器即可。 它应该是:

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

不是

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

注意: jQuery 就绪事件似乎触发了多次。 确保这符合你的逻辑。

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. Make sure that is OK with your logic.

幻梦 2024-07-15 15:50:33

这个答案可能会迟到,但这个回复可能会帮助像我这样的人...

这可以通过本机 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

This answer may be late, but this reply may help someone like me...

This can be done via native Javascript code -

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
行至春深 2024-07-15 15:50:33

没有理由不调用 $(document).ready()
确保您的页面包含 jquery.js 的包含内容。 尝试使用空 HTML 页面进行简单测试,仅发出警报以查看是否存在其他问题。

如果您尝试在包含框架定义的 HTML 页面中使用它,请记住那里没有文档,您将必须使用

There is no reason for $(document).ready() not to be called.
Be sure your page contains an include to jquery.js. Try to do a simple test with an empty HTML page and just an alert to see if there is another problem.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文