javascript 从 iframe 内部调用父 jquery 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14915214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:54:02  来源:igfitidea点击:

Call parent jquery function from inside an iframe?

javascriptjquery

提问by user1978483

Let's say I have this page "index.php" and in it I have this link that calls a pop up box:

假设我有这个页面“index.php”,其中我有一个调用弹出框的链接:

<a href="#" id="signinlink">Sign in</a>

the jquery function is:

jquery 函数是:

<script type="text/javascript" charset="utf-8">
    jQuery(function() {
        function launch() {
             jQuery('#sign_up').lightbox_me({centered: true, onLoad: function() { jQuery('#sign_up').find('input:first').focus()}});
        }
        jQuery('#signinlink').click(function(e) {
            jQuery("#sign_up").lightbox_me({centered: true, onLoad: function() {
                jQuery("#sign_up").find("input:first").focus();
            }
        });
    });
</script>

How from iframe to call this function?

如何从 iframe 调用这个函数?

(* the iframe is called from parent page "index.php") I think i must add a link with

(* iframe 是从父页面“index.php”调用的)我想我必须添加一个链接

window.parent.callme();

But how? please help!

但是如何?请帮忙!

回答by naugtur

First of all - if the iframe is something to do with login form - it should send its own content itself. The code should be there, not get called in the parent frame.

首先 - 如果 iframe 与登录表单有关 - 它应该自己发送自己的内容。代码应该在那里,而不是在父框架中被调用。

Anyway, your question is missing some details on what exactly you want to do.

无论如何,您的问题缺少有关您究竟想做什么的一些细节。

To access stuff from parent you do:

要从父母那里访问东西,你可以:

window.parent.functionname()instead of just functionname()

window.parent.functionname()而不仅仅是 functionname()

So

所以

$(document).find(...)would become window.parent.$(window.parent.document).find(...)

$(document).find(...)会成为 window.parent.$(window.parent.document).find(...)

To access the iframe from the parent, you need to fetch contentDocument of the iframe DOM node.

要从父级访问 iframe,您需要获取 iframe DOM 节点的 contentDocument。

This is also helpful: How to expose IFrame's DOM using jQuery?

这也很有帮助:如何使用 jQuery 公开 IFrame 的 DOM?

If you can make your question clearer, it might be possible to show you an example of code that works with what you have.

如果您可以让您的问题更清楚,则可能会向您展示一个适用于您所拥有的代码的示例。

回答by mplungjan

This works for me - but only if I set jQuery to 1.8 OR add jQuery migratesince the lightbox-me needs the deprecated in 1.8 and deleted in 1.9 browser detection code

这对我有用 - 但只有当我将 jQuery 设置为 1.8 或添加jQuery migrate 时,因为 lightbox-me 需要在 1.8 中弃用并在 1.9 浏览器检测代码中删除

DEMO

演示

in iframe (assuming no jQuery in iFrame):

在 iframe 中(假设 iFrame 中没有 jQuery):

<a id="parentsignin" href="#">Signup</a>

using

使用

window.onload=function() {
  document.getElementById("parentsignin").onclick=function() {
    parent.document.getElementById('signinlink').click();
    return false;
  }
}


Actually, you might want to do this in parent - notice I cancel the link itself with preventDefault and reuse the code:

实际上,您可能希望在父级中执行此操作 - 请注意,我使用 preventDefault 取消了链接本身并重用了代码:

function launch() {
  $('#sign_up').lightbox_me({ 
    centered: true, 
    onLoad: function() { 
      $('#sign_up').find('input:first').focus();
    }
  });
}


$(function() {
  $('#signinlink').click(function(e) {
    e.preventDefault(); // stop the link from being followed
    launch(); 
  });
});