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

时间:2017-07-21 11:31:10

标签: jquery parent-child access

(编辑)所以我已经包含了完整的测试代码,但最初我认为有一些方法可以从孩子那里知道父母点击的元素而不从父母那里手动传递这些信息。在此示例中,我将该值保存到“新页面”中。要在子页面上访问的变量。除了这个方法并通过url查询字符串传递变量,还有其他方法将信息传递给child ??

(原始问题) 让我说我从父片段中得到一些简单的东西,一旦我在我的子窗口中点击它就会触发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>

然后在子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>

所以在这个例子中,我首先点击了第二个&#39;点击我&#39;来自parent.html使用data-quoteid =&#34; yahooquote123&#34;

我希望提醒我,而不是使用window.opener。$(&#39;#test2&#39;)。attr(&#39; data-quoteid&#39;),因为我不喜欢#39; t想要通过任何id来选择,而是通过被点击的元素来原始打开孩子。

0 个答案:

没有答案