gpt4 book ai didi

javascript - 如何在子窗口中获取父窗口标签的html?

转载 作者:行者123 更新时间:2023-12-03 03:30:06 24 4
gpt4 key购买 nike

我通过以下代码从父窗口打开一个子窗口:

<html>
<head>

</head>

<script type="text/javascript">
function foo(){
window.open("child.html","child window");
}
</script>

<body id="data">

<img src = "2.jpg">
<button id="button1" name="NiceButton" onclick="foo()">click here</button>


</body>
</html>

在子窗口中,我想获取父窗口 body 标签的 html 。所以我使用以下代码:

        <html>
<head>

</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript" src="html2canvas.min.js"></script>
<script type="text/javascript" language="javascript" src="html2canvas.js"></script>

<script type="text/javascript">

function capture() {
window.alert('test');

html2canvas(window.opener.$("body")
,{
onrendered: function (canvas) {
var imgString = canvas.toDataURL("image/png");
window.open(imgString);

}
}
);
}
</script>

<body id="d" onload="capture()">

<button id="button1" name="NiceButton" onclick="capture()">click here</button>

</body>
</html>

但是我收到以下错误:

TypeError: window.opener.$ is not a function

如何解决此错误?

我需要什么:我需要获取父窗口 body 标记的innerhtml。以下代码可以工作吗?

alert($("window.opener.body")) ; 

最佳答案

将您的捕获函数替换为

function capture() {
window.alert('test');

html2canvas(window.opener.document.body
,{
onrendered: function (canvas) {
var imgString = canvas.toDataURL("image/png");
window.open(imgString);

}
}
);
}

关于javascript - 如何在子窗口中获取父窗口标签的html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com