我正在尝试打开这样的弹出窗口:
$('#btn').click(function () {
var popup = window.open('mypage.php', '_blank', 'width=500,height=500');
var dom = popup.document.body;
for (i in dom) {
console.log(dom[i]);
}
});
现在我想做的是从弹出窗口中获取 html,并且还能够使用 window.opener(打开弹出窗口的页面)中的 jQuery 函数
附言。控制台打印了很多东西,但是没有html源。
用这个试试:http://jsfiddle.net/JRqTy/
提前致谢。
最佳答案
尝试:
var html = popup.document.documentElement.outerHTML
编辑
窗口不会立即加载。假设您没有试图违反 same-origin policy,这样的事情会起作用。 :
$('#btn').click(function() {
var popup = window.open('[Your URL HERE]', '_blank', 'width=500,height=500');
popup.onload = function() {
setTimeout(function(){ console.log(popup.document.documentElement.outerHTML) }, 2000);
}
});
这是 a working fiddle .
注意:如果您同时控制父源和子源,您还可以让子源调用传递给它的 html 的父源的方法:
子窗口
// Call when body is loaded
function SendHtmlToParent() {
window.opener.HtmlReceiver(document.outerHTML);
}
父级
function HtmlReceiver(html) {
console.log(html);
}
关于javascript - 如何使用jquery从window.open js函数获取元素和html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9810035/