13

当我打开的子窗口的文档已加载并准备好时,我试图得到通知。这似乎不起作用:

win = window.open(href, 'test', 'width=300, height=400');
win.focus();
$(win.document).ready(function() {
           // Ok, the function will reach here but if I try to manipulate the
           // DOM it doesn't work unless I use breakpoints
           $(this).contents().find("...").doStuff(); // nothing happens
    });

我需要做什么?

4

4 回答 4

10

你试过这个吗?——</p>

$(win.document).ready(function() {
    $(win.document).contents().find("...").doStuff();
});

这个问题讨论了非常相似的事情。复制?

于 2011-01-30T11:29:24.260 回答
6

我遇到了类似的问题,对我来说,窗口上的 .load 事件确实有效,而 .ready 没有。所以你可以试试:

win = window.open(href, 'test', 'width=300, height=400');
$(win).load(function() {
    $(this).contents().find("...").doStuff(); 
});
于 2013-02-19T16:03:16.947 回答
1

window.opener在站点脚本中使用,您正在加载并执行在第一页全局(!)中定义的函数。

主页:

<html>
<head>
<script type="text/javascript">
    window.notify = function () {
        alert('runned from opened window');
    };
    window.onload = function() {
        document.getElementById('button').addEventListener('click', function() {
            window.open('test.html');
        }, false);
    };
</script>
</head>
<body>
<button id="button">Open window</button>
</body>

打开页面:

<html>
<head>
<script type="text/javascript">
    window.onload = function() {
        window.opener.notify()
    };
</script>
</head>
<body>
    Popup site
</body>
</html>
于 2011-01-30T11:53:37.090 回答
0

只需在 iframe 中添加此代码,

$(document,parent.document).ready(function(){
alert('Done');
});  
于 2016-12-14T13:23:34.603 回答