jquery访问窗口的奇怪bug

Strange bug with jquery accessing window

本文关键字:bug 访问 窗口 jquery      更新时间:2023-09-26

我有这个代码http://jsfiddle.net/xxL6e2fk/

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
janela = window.open("https://www.sitepor500.com.br");
window.setTimeout(
    function() {
        alert($(janela.window.document).text());
        alert($(janela.window.document).html());
    },
    5000
);
</script>

它只是打开一个窗口,并试图获得其内容的"文本"answers"html"。"文本"显示正确的内容,但"html"不是。有人知道怎么解决这个问题吗?

janela.window.document上没有html,尝试使用janela.window.document.documentElement代替或在新窗口内瞄准body标签。

$("body", janela.window.document.documentElement).html()

你可以这样写:

var janela = window.open('https://www.sitepor500.com.br');
janela.onload = function () {
    setTimeout(function () {
        console.log(janela.document.documentElement.outerHTML)
    }, 2000);
}

但是只有当你从相同的域(sitepor500.com.br)