$ 未在 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>
if 那么我如何在 iframe 页面中使用 jquery 而不包含这一行?
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".
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>
if so how can I use jquery in the iframe page without including this line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想通了。在我使用的 iFrame 中
,以及为任何 jquery 调用文档提供上下文。这
很奇怪,因为我原来的方式(两个页面都包含 jquery)在 Safari for Mac 中运行良好,但在 Firefox 中却给了我错误
I figured it out. In the iFrame I used
as well as giving any jquery calls the document for context. ie
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
我正在网页中加载 iframe,我可以找到 $ 对象
,但这并没有对 iFrame 的元素执行选择。发现下面的代码在 iFrame 主体上执行选择器
不知何故,文档对象也是
未定义
。上述代码来源: https://gist.github.com/843229
希望对某人有所帮助。
I was loading iframe in a webpage and I could find $ object using
but this was not performing selection on the elements of iFrame. Found below code to execute selector on the iFrame body
Somehow the document object was also
undefined
.Source of above code: https://gist.github.com/843229
Hope it helps someone.
坏消息——除非文档中存在 Jquery,否则您无法使用它。所以,无论如何,你都必须加载它。
实际上,在两个文档中都包含 jquery 调用并没有什么坏处。我有几个客户,他们使用 iFrame 作为我公司营销自动化应用程序的一部分(其中包含该应用程序),并且还在其父文档中包含 Jquery,并且它工作得很好,没有冲突。从负载的角度来看,假设您设置正确,由于缓存,命中可以忽略不计。所以,包括在内。
iFrame 的经验法则...将它们视为页面内的单独页面。是的,在某些情况下,事情会延续下去,但文档起源模型会阻止 CSS 等内容在父级和子级之间交叉。
Bad news-- you can't use Jquery unless it's in the document. So, you're gonna have to load it regardless.
There's actually no harm in including the jquery call in both documents. I have several clients who iFrame a portion of my company's app for marketing automation (which has it included) and also have Jquery included in their parent document, and it works just fine with no conflicts. From a load standpoint, assuming you're setup correctly, the hit will be negligible due to caching. So, include away.
Rule of thumb on iFrames...think of them as separate pages inside your page. Yes, in some cases things carry over, but the document origin model prevents things like CSS from crossing between parent and child.
升级到较新版本 https://code.jquery.com/jquery-2.1 .3.min.js
Upgrade to newer version f.e. https://code.jquery.com/jquery-2.1.3.min.js