使用javascript,我可以按如下方式打开和关闭弹出窗口。
var myWindow = window.open('http://example.org/');
// do things to window.
myWindow.close();
如何在关闭之前在弹出窗口中操作页面的DOM元素?我试过了
myWindow.onready = function() {
myWindow.document.getElementById('someElementID').style.color = '#f00';
}
无济于事。
答案 0 :(得分:3)
没有任何事件可以让您知道在弹出窗口中正确加载了URL。唯一可行的选择是设置超时,然后访问元素。
像这样:
var myWindow = window.open('./t6.html');
setTimeout(check, 3000);
console.log($('body', myWindow.document)); // This shows a blank "body"
function check() {
console.log($('body', myWindow.document)); // This shows the actual elements in the document
}
myWindow.close();
答案 1 :(得分:1)
Here's a fiddle表明它在没有现成的处理程序的情况下工作。
var myWindow = window.open('http://example.org/');
alert(myWindow)
// Alerts an HTMLDocument object
与评论中提到的@jfriend00一样,Window object没有现成的事件处理程序。