js函数中获取元素和html

js函数中获取元素和html

本文介绍了如何使用jquery从window.open js函数中获取元素和html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开这样的弹出窗口:

I am trying to open a popup like this:

$('#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函数(打开弹出窗口的页面)

Now what I want to do is to get the html from the popup window and also be able to use maybe a jQuery function from the window.opener (the page that opened the popup)

PS。在控制台中有很多东西打印但没有html源。

PS. In the console there are a lot of things printed but no html source.

使用它来尝试:

提前致谢。

推荐答案

尝试:

var html = popup.document.documentElement.outerHTML

编辑

窗口未立即加载。假设您没有试图违反,则此类内容将起作用:

The window is not loaded immediately. Something like this will work, assuming that you're not attempting to violate the 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);
    }
});​

这是。

注意:如果您同时控制父源和子源,您也可以让子进程调用传递给它的父进程的html:

Note: If you control both the parent and the child source, you could also have the child invoke a method on the parent that passes in it's html:

子窗口

// Call when body is loaded
function SendHtmlToParent() {
    window.opener.HtmlReceiver(document.outerHTML);
}

父母

function HtmlReceiver(html) {
    console.log(html);
}

这篇关于如何使用jquery从window.open js函数中获取元素和html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:54