如何从子窗口jquery调用父窗口函数? [英] How to call parent window function from child window jquery?

查看:166
本文介绍了如何从子窗口jquery调用父窗口函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户专注于子窗口时,我只需要在父窗口中调用函数。
我的父窗口中有此代码,

i just need to call function in parent window while user is focusing on child window. i have this code in my parent window,

<html> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="Javascript" type="text/javascript">
        function CallParent()
        {
            alert(" Parent window Alert");
        }
    </script>
    <body> 
        <a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
    </body> 
</html>

以及波纹管代码在我的子窗口中,

and bellow code is in my child window,

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script language="Javascript" type="text/javascript">
            jQuery(document).ready(function(){  
                window.opener.CallParent();
            });
        </script>
    </head>
    <body> 
        <h2>This is Child window</h2> 
    </body> 
</html>

so..in在这种情况下,我认为 CallParent()将被触发。但它似乎不起作用。
可以让任何人给我任何提示,让这个脚本能够工作,或者有更好的方式来做到这一点。

so..in this case i supposed that CallParent() will be fired just after child window is opened. but it seems to be not working. can any one give me any hints to make this script to work, or any better way to do this.

推荐答案

尝试以下内容:

parent.html

<html> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="Javascript" type="text/javascript">
        window.CallParent = function() {
            alert(" Parent window Alert");
        }
    </script>
    <body> 
        <a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("child.html","Ratting","width=550,height=170,0,status=0,");>Click here to open the child window</a>
    </body> 
</html>

child.html

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script language="Javascript" type="text/javascript">
            jQuery(document).ready(function(){  
                opener.CallParent();
            });
        </script>
    </head>
    <body> 
        <h2>This is Child window</h2> 
    </body> 
</html>

你不应该在父对象上执行此操作,否则opener.CallParent()将不起作用, .CallParent使CallParent可用作窗口范围:

You should not do this on the parent, otherwise opener.CallParent() will not work, doing window.CallParent makes CallParent available as window scope:

function CallParent() {
  alert(" Parent window Alert");
}

然后您可以简单地调用 opener.CallParent ); 不是 window.opener.CallParent();

And then you can simply call opener.CallParent(); not window.opener.CallParent();.

这篇关于如何从子窗口jquery调用父窗口函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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