JQuery 和框架 - $(document).ready 不起作用
我有一个页面,其中有一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我已经尝试过另一条评论中提到的方法:
但它对我不起作用。
这样做是这样的:
尽管事件没有那么快触发 - 它会等到图像和 CSS 也加载完毕。
I have tried the method mentioned in another comment:
and it did not work for me.
this did:
Even though the event does not fire as quickly - it waits until images and css have loaded also.
我知道这是一个老话题了。 但为了帮助一些到达此页面的人,这是我的解决方案:
I know this is an old topic. But to help some of you who reach this page, here is my solution:
我认为这与我在 iframe 中使用 DOMContentLoaded 时遇到的类似问题。
我写了一篇关于它的博客文章。
I assume this is a similar problem I was having with DOMContentLoaded in an iframe.
I wrote a blog post about it.
如果您想为框架触发
onload
事件,请按照以下步骤操作:为每个
分配一个
标签。 确保id
和name
>id
和name
属性值相同。使用以下代码触发框架的
onload
事件:If you want to fire the
onload
event for your frames, then follow these steps:Assign an
id
andname
to each<frame>
tag. Make sure bothid
andname
attributes value is same.Use the following code to fire the
onload
event of the frame:以下内容也对我有用:
[0] 表示它是文档中的第一帧,[1] 表示第二帧,依此类推。 如果您无法控制标记,并且它仍在使用文档就绪,那么这尤其好。
The following also worked for me:
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.
您是否尝试过将 jQuery 代码放入 Info.aspx 页面中?
Have you tried to put the jQuery code inside the Info.aspx page?
不知道你想做什么,但我有一个更旧的经典 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.
我在这篇文章上工作了很长时间......这是我的解决方案。
测试.html
测试2.html
I have worked a long time with this post... here is my solution.
test.html
test2.html
我不知道这是否是最好的解决方案,但是当我删除
$(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.无需修改标记。 只需修复选择器即可。 它应该是:
不是
注意: jQuery 就绪事件似乎触发了多次。 确保这符合你的逻辑。
No need to modify the markup. Just fix the selector. It should be:
not
Note: it seems the jQuery ready event fires multiple times. Make sure that is OK with your logic.
这个答案可能会迟到,但这个回复可能会帮助像我这样的人...
这可以通过本机 Javascript 代码来完成 -
This answer may be late, but this reply may help someone like me...
This can be done via native Javascript code -
没有理由不调用
$(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