我有一个带有 iframe 的页面。我的 iframe 页面是 iframe.php,我的主页是 main.php 当我直接加载 iframe.php 时,我的 jquery 代码执行正常,但是当我加载 main.php (其中包含 iframe.php 作为 iframe)时,我收到错误“$ 未定义”。
这可能是因为 main.php 和 iframe.php 都使用了
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
如果是这样,我如何在 iframe 页面中使用 jquery 而不包含此行?
最佳答案
我明白了。在 iFrame 中我使用了
var $ = parent.$;
以及为任何 jquery 调用文档提供上下文。即
$("#element", document).doStuff();
这很奇怪,因为我原来的方式(两个页面都包含 jquery)在 Mac 版 Safari 中运行良好,但在 Firefox 中却出现错误
Error: c.defaultView.getComputedStyle(h, null) is null Source File: http://code.jquery.com/jquery-latest.pack.js Line: 16
关于jquery - $ 未在 iFrame 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5986960/