如何从子窗口jQuery调用父窗口函数?

2021/01/31 15:51 · jquery ·  · 0评论

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

<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>

下面的代码在我的子窗口中,

<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>

所以..在这种情况下,我认为那CallParent()将在打开子窗口后立即触发。但它似乎不起作用。任何人都可以给我任何使该脚本正常工作的提示,或任何更好的方法来执行此操作。

用这个

window.parent.CallParent();

代替

window.opener.CallParent();

window.parent 保存对当前窗口或子帧的父级的引用。

如果窗口没有父窗口,则其parent属性是对自身的引用。

当在一个被加载的窗口<iframe><object>或者<frame>,它的父是与元件嵌入窗口的窗口。

参考:https : //developer.mozilla.org/en-US/docs/Web/API/Window/parent

尝试如下所示:

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()在运行window时将不起作用。CallParent使CallParent可用作窗口范围:

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

然后您可以简单地opener.CallParent();打电话window.opener.CallParent();

我使用了以下代码
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">
        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>

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">
           window.opener.CallParent();
        </script>
    </head>
    <body> 
        <h2>This is Child window</h2> 
    </body> 
</html>
本文地址:http://jquery.askforanswer.com/ruhecongzichuangkoujquerydiaoyongfuchuangkouhanshu.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!