从iframe访问jQuery库 [英] Access jQuery library from iframe

查看:126
本文介绍了从iframe访问jQuery库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用iframe的遗留应用程序。
它在父页面中有一个iframe,它被其他页面动态替换。

I have a legacy application that uses iframes. It has an iframe in the parent page, that is dynamically replaced with other pages.

JQuery加载在父页面中。是否有某种类型的插件允许我访问从iframe页面加载到父级的jquery核心,而不包括jquery
(language =JavaScriptsrc =../javascript / jquery.js )在多个子(iframe)页面?

JQuery is loaded in the parent. Is there some type of plugin that will allow me to access the jquery core that is loaded in the parent from the iframe pages without including jquery (language="JavaScript" src="../javascript/jquery.js") in the multiple child (iframe) pages?

例如,iframe是静态的

For example, the iframe is static

<iframe name="mainWindow" src="includes/View.asp frameborder="0" />

我知道有更好的方法可以做到这一点,但我现在仍然坚持使用这种架构。
有什么建议吗?

I know there are better ways to do this, but I am stuck with this architecture at the moment. Any suggestions?

推荐答案

您可以尝试从iframe内部运行:

You could try running this from inside your iframe:

var $ = jQuery = window.parent.$;

假设父级和iframe位于同一个域中。

Assuming the parent and iframe are on the same domain.

刚试过它,例如: http://jsfiddle.net / qA7yE /

Just tested it, example: http://jsfiddle.net/qA7yE/

(该示例使用不同的鳕鱼e - 子iframe调用父的 foo()函数,但同样的原则适用)

(That example uses different code - the child iframe calls the parent's foo() function, but the same principle applies)

另外,只是为了安全起见,你可能想做:

Also, just to be on the safe side you may want to do:

var $ = window.parent.$, jQuery = window.parent.jQuery;

这篇关于从iframe访问jQuery库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆