简体   繁体   English

如何将Jquery对象从父iframe注入到嵌入式iframe?

[英]How can I inject a Jquery object from a parent iframe to an embedded iframe?

I am embedding an iframe in a webpage and I want to inject jquery in this frame. 我将iframe嵌入网页中,并且想在此框架中插入jquery。

I tried something like: 我尝试了类似的东西:

frame.contentWindow.$ = window.$

While this works, the iframe's $ is still running in the parent's scope as opposed to its own independent copy. 在此过程中,iframe的$仍在父级范围内运行,而不是其自己的独立副本。

Is there a way to inject an independent jquery object into the iframe without injecting a <script src="/path/to/jquery.js"></script> in the frame? 有没有一种方法可以在iframe中注入独立的jquery对象而无需在帧中注入<script src="/path/to/jquery.js"></script>

UPDATE UPDATE

Solved it. 解决了。 The jquery.js conveniently defines jQuery for a scope by accepting the window object in an anonymous function: 通过在匿名函数中接受window对象,jquery.js方便地为范围定义了jQuery:

function(windowObject){ .... define jquery }(window)

So all I had to do was to name this function, Eg jQuerify and call it once for the parent window object and then for the child window object. 因此,我所要做的就是命名此函数,例如jQuerify,并先对父窗口对象然后对子窗口对象调用它。

jQuerify = function(windowObject){ .... define jquery }
jQuerify(window)
jQuerify(iframe.contentWindow)

Solved it. 解决了。 The jquery.js conveniently defines jQuery for a scope by accepting the window object in an anonymous function: 通过在匿名函数中接受window对象,jquery.js方便地为范围定义了jQuery:

function(windowObject){ .... define jquery }(window) So all I had to do was to name this function, Eg jQuerify and call it once for the parent window object and then for the child window object. function(windowObject){.... define jquery}(window)因此,我要做的就是命名该函数,例如jQuerify并为父窗口对象调用一次,然后为子窗口对象调用一次。

jQuerify = function(windowObject){ .... define jquery }
jQuerify(window)
jQuerify(iframe.contentWindow)

You can try cloning the jQuery object: 您可以尝试克隆jQuery对象:

See this answer or this one on how to implement a .clone function, then call this: 看到这个答案还是这一个关于如何实现一个.clone函数,然后再调用这个:

iframe.contentWindow.$ = window.$.clone();

or 要么

iframe.contentWindow.$ = window.$.bind({});

... depending on how you choose to clone the function. ...取决于您选择克隆功能的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM