如何访问已单击的父窗口属性(使用jquery)?

如何访问已单击的父窗口属性(使用jquery)?,jquery,parent-child,Jquery,Parent Child,(已编辑)因此我包含了完整的测试代码,但最初我认为有某种方法可以从子级了解父级的clicked元素,而无需手动从父级传递该信息。在本例中,我将该值保存到“newpage”变量中,以便在子页面上访问。除了此方法和通过url查询字符串传递变量外,还有其他方法将信息传递给子对象吗 (原问题) 假设我有一个来自父代码段的简单内容,当我进入我的子窗口并单击它时,将触发newWindow2事件,我希望访问父元素的单击元素数据quoteid parent.html: <html> <head

(已编辑)因此我包含了完整的测试代码,但最初我认为有某种方法可以从子级了解父级的clicked元素,而无需手动从父级传递该信息。在本例中,我将该值保存到“newpage”变量中,以便在子页面上访问。除了此方法和通过url查询字符串传递变量外,还有其他方法将信息传递给子对象吗

(原问题) 假设我有一个来自父代码段的简单内容,当我进入我的子窗口并单击它时,将触发newWindow2事件,我希望访问父元素的单击元素数据quoteid

parent.html:

<html>
<head>
    <title></title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript">
        var windowSizeArray = [ "width=200,height=200",
                                "width=300,height=400,scrollbars=yes" ];

        var newpage;

        $(document).ready(function(){
            $('.newWindow').click(function (event){

                var url = $(this).attr("href");
                var windowName = "popUp";//$(this).attr("name");
                var windowSize = windowSizeArray[$(this).attr("rel")];

                x = $(this).closest('li').attr('data-quoteid');
                                  alert(x);

                newpage = x;
                window.open(url, windowName, windowSize);

                event.preventDefault();

            });
        });

    </script>
</head>

<body>
    <ul>
        <li id="test1" name="googlename" data-quoteid="googlequote123">
            <a href="open.html" rel="0" class="newWindow" >click me</a>
        </li>
        <li id="test2" name="yahooname" data-quoteid="yahooquote123">
            <a href="open.html" rel="1" class="newWindow" >click me</a>
        </li>
    </ul>
</body>

var WINDOWSIZERRAY=[“宽度=200,高度=200”,
“宽度=300,高度=400,滚动条=是”];
var newpage;
$(文档).ready(函数(){
$('.newWindow')。单击(函数(事件){
var url=$(this.attr(“href”);
var windowName=“popUp”//$(this.attr(“name”);
var windowSize=windowsizerray[$(this.attr(“rel”)];
x=$(this.nexist('li').attr('data-quoteid');
警报(x);
newpage=x;
打开(url、windowName、WindowsSize);
event.preventDefault();
});
});

然后在child open.html中

<html>
<head>
    <title></title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript">
        var windowSizeArray = [ "width=200,height=200",
                                "width=300,height=400,scrollbars=yes" ];

        $(document).ready(function(){
            $('.newWindow2').click(function (event){

                 if(window.opener && !window.opener.closed) {
                        alert(window.opener.newpage);
                    }                    

                event.preventDefault();

            });
        });


    </script>
</head>

<body>
    <div name="divnamehere">test details here</div>


    <a href="#" class="newWindow2">click me</a>
</body>

var WINDOWSIZERRAY=[“宽度=200,高度=200”,
“宽度=300,高度=400,滚动条=是”];
$(文档).ready(函数(){
$('.newWindow2')。单击(函数(事件){
if(window.opener&!window.opener.closed){
警报(window.opener.newpage);
}                    
event.preventDefault();
});
});
测试细节在这里

因此,在本例中,我首先使用data quoteid=“yahooquote123”单击parent.html中的第二个“click me”


我希望警报返回给我,而不是使用window.opener.$(“#test2”).attr('data-quoteid'),因为我不想通过任何id进行选择,而是通过最初单击以打开子项的元素进行选择。

您的弹出窗口不知道这两个链接中的哪一个被单击以打开它。你只有在parent.html中才能找到这些信息,因此你需要在那里处理点击事件并存储这些信息。顺便说一句,使用如图所示的代码,你在parent.html中的链接甚至不会打开任何新窗口,您遗漏了实际实现此功能的部分。当您打开新窗口并分析子页面中的url时,还可以在url的查询字符串中传递
quoteid