jquery - $ 未在 iFrame 中定义

标签 jquery iframe

我有一个带有 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/

相关文章:

JavaScript 不安全访问?

javascript - 如何使用样式表打印 iFrame?

jquery - jquery中通过样式选择器查找元素

php - jQuery 在 javascript 中序列化

javascript - 如何从 diff 源获取 iframe html

javascript - 加载前在 Iframe 中附加用户名?

javascript - 我的脚本只适用于台式机,不适用于移动设备。你能告诉我为什么吗?

javascript - 触摸屏中的事件

javascript - JS增加/减少多个输入数字变量

javascript - 如何显示在 iframe 中呈现的 html 文件的源代码