Jquery在父窗口中从子窗口运行函数

发布于 2024-10-09 21:16:11 字数 371 浏览 0 评论 0原文

我有一个弹出窗口,我想从父窗口中运行一个函数。

在我的孩子中:

$(document).ready(function(){
    parent.$.fn.guestFromPop('Action');
});

在我的父母中:

$(document).ready(function(){
    function guestFromPop(data) {
        alert(data);
    }
});

问题是当 parent.$.fn.guestFromPop('Action'); 我从未看到警报时,什么也没有发生。我做错了什么吗?

I have a popup window and I want to run a function from in the parent window.

In the child I have:

$(document).ready(function(){
    parent.$.fn.guestFromPop('Action');
});

In the parent I have:

$(document).ready(function(){
    function guestFromPop(data) {
        alert(data);
    }
});

The problem is when parent.$.fn.guestFromPop('Action'); I never see the alert, nothing happens. Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静若繁花 2024-10-16 21:16:11

这不会声明函数 $.fn.guestFromPop

$(document).ready(function(){
  function guestFromPop(data) {
    alert(data);
  }
});

...它所做的只是声明 guestFromPop(),一个仅在该 文档中可用的函数。准备好处理程序。您需要声明您想要的函数:

$.fn.guestFromPop = function(data) {
  alert(data);
};

不过,这也不是真正正确的,因为它不会在 jQuery 元素上调用,您可能想要像 : 这样简单的东西,

function guestFromPop(data) {
  alert(data);
}

通过以下方式调用:

parent.guestFromPop('Action');

This doesn't declare the function $.fn.guestFromPop:

$(document).ready(function(){
  function guestFromPop(data) {
    alert(data);
  }
});

...all it does is declare guestFromPop(), a function only available inside that document.ready handler. You would need to declare the function you're after instead:

$.fn.guestFromPop = function(data) {
  alert(data);
};

Though, this isn't really correct either, since it wont be called on a jQuery element, you may want something as simple as :

function guestFromPop(data) {
  alert(data);
}

called by:

parent.guestFromPop('Action');
a√萤火虫的光℡ 2024-10-16 21:16:11
(function(){
$.fn.TestFunction=function(SampleData)
{
alert(SampleData);
};
});

//可在其他父页面或iframe中调用
//

(function(){
$.fn.TestFunction("sdfsdfsfsfsd");

});
(function(){
$.fn.TestFunction=function(SampleData)
{
alert(SampleData);
};
});

//callable in other parent page or iframe
//

(function(){
$.fn.TestFunction("sdfsdfsfsfsd");

});
心不设防 2024-10-16 21:16:11

我也有同样的问题。想通了。您的代码的问题是您没有引用正确的窗口来调用该函数。要从控制台到达其父控制台,您需要“opener”,而不是“parent”。

因此,您需要调用以下函数,而不是parent.guestFromPop('Action'):

$(self.window.opener.document).guestFromPop('Action');

That 应该引用正确的函数。这对我有用! ;-)

I had the same problem. Figured it out. The problem with your code is that you're not referring to the right window to call the function from. To get from a console to it's parent you'll need 'opener', not 'parent'.

So instead of parent.guestFromPop('Action'), you need to call this:

$(self.window.opener.document).guestFromPop('Action');

That should refer to the correct function. It worked for me! ;-)

回心转意 2024-10-16 21:16:11
guestFromPop = function(data) {
  alert (data)
}

从 iframe 调用:

parent.guestFromPop(data);
guestFromPop = function(data) {
  alert (data)
}

call from iframe:

parent.guestFromPop(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文