$未在iFrame中定义 [英] $ is not defined in iFrame
问题描述
我有一个包含iframe的页面。我的iframe页面是iframe.php而我的主页面是main.php,当我直接加载iframe.php我的jquery代码执行正常,但是当我加载main.php(其中包含iframe.php作为iframe)时,我收到错误 $未定义。
I have a page with an iframe in it. my iframe page is iframe.php and my main page is main.php when i load iframe.php directly my jquery code executes fine, but when I load main.php (which contains iframe.php as an iframe) I get an error "$ is not defined".
这可能是因为main.php和iframe.php都使用
could this be because both main.php and iframe.php use
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
如果是这样,如何在iframe页面中使用jquery而不包括此行?
if so how can I use jquery in the iframe page without including this line?
推荐答案
我弄清楚了。在我使用的iFrame中
I figured it out. In the iFrame I used
var $ = parent.$;
以及为上下文提供任何jquery调用文档。 ie
as well as giving any jquery calls the document for context. ie
$("#element", document).doStuff();
这很奇怪,因为我拥有它的原始方式(两个页面都包含jquery)在Safari中运行良好对于Mac但在Firefox中它给了我错误
It's weird because the original way I had it (with jquery included in both pages) worked fine in Safari for Mac but in Firefox it gave me the error
错误:c.defaultView.getComputedStyle(h,null)为null源文件:< a href =http://code.jquery.com/jquery-latest.pack.js =noreferrer> http://code.jquery.com/jquery-latest.pack.js 专线:16
这篇关于$未在iFrame中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!