iframe 与 jquery
有没有人曾经有过一个带有 iframe 的网页,其中使用了 jquery?我的包含 iframe 的主页包含 jquery
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
,并且没有 javascript 错误。我的 iframe 以同样的方式包含 jquery,如果我自己加载它,它也没有 javascript 错误。但是,当我加载带有 iframe 的主页时,出现两个错误
错误:c.defaultView.getCompulatedStyle(h, null) 为 null 源文件: http://code.jquery.com/jquery-latest.pack.js< /a> 线路:16
路
错误:$ 未定义 源文件: http://myiframe.php 线路:23
如果 iframe 中只是普通的 javascript,则可以正常工作
has anyone ever had a webpage with an iframe in it that used jquery? My main page that contains the iframe includes jquery with
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
and it has no javascript errors. My iframe includes jquery the same way and if I load it by itself it also has no javascript errors. However when I load my main page with the iframe in it I get two errors
Error: c.defaultView.getComputedStyle(h, null) is null
Source File: http://code.jquery.com/jquery-latest.pack.js
Line: 16
and
Error: $ is not defined
Source File: http://myiframe.php
Line: 23
if it is just plain javascript in the iframe it works fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相当确定(?呃...)如果您在同一域上使用 JQUERY,则无需在 iFrame 中加载的页面中再次加载它。您只需在父页面中加载一次即可。
我知道,当您单独测试页面(即不在 iFrame 中)时,您可能需要使其可用,但您可以在发布之前禁用
标记。
我在让 javascript 遍历 iFrame 进入主父文档并再次返回 iframe(一般情况下)时遇到了可怕的问题,并记得必须执行诸如
parent.document
这样的事情才能让 js 一起对话。您可能需要考虑使用 jquery
.load()
将同一域页面的内容加载到(滚动)中,这再次避免了重新加载所有内容您的 javascript 和 CSS(如果您已经在父页面上完成了这些操作)。我发现这有时非常有用。
I'm fairly sure (? erm...) that if you are using JQUERY on the same domain, you do not need to load it a second time in the page that you are loading in the iFrame. You can just load it in the parent page once.
I know that you probably need to have it available when you are testing the page in isolation (ie not in the iFrame) but you can disable the
<script>
tag before you publish.I've had terrible problems making javascript traverse iFrames into the main parent document and back to the iframe again (in general) and recall having to do things like
parent.document
just to get the js to talk together.You may want to consider loading the contents of the same domain page into a (scrolling)
<div>
using jquery.load()
, which again avoids having to reload all your javascripts and CSS if you've already done that on the parent page. I've found this to be quite useful at times.