嗨,我在阅读使用 window.open()
打开的窗口内的文本时遇到了一些麻烦。我希望你们能帮助我,我需要的是我的父窗口会读到 <div>
父窗口调用的子窗口的 html 值,
我有这段代码,但它不起作用
父窗口
<button onclick="buttonset()">Click me</button>
<script>
var myWindow ;
function buttonset () {
myWindow = window.open("pag1.html", "myWindow", "width=200,height=100");
//myWindow.document.write("<div id='hola'>This is 'myWindow'</div>");
alert(myWindow.document.getElementById("result").innerHTML);
myWindow.close();
}
</script>
这是我的子窗口(pag1.html)代码
<body>
<div id="result">1</div>
<div id="msj">Correcto</div>
</body>
当我运行时,它说“只需打开新窗口”,但它不显示消息
Cannot read property 'innerHTML' of null
最佳答案
window.open()
是异步的,需要等待文档加载。
function buttonset () {
var myWindow = window.open("pag1.html", "myWindow", "width=200,height=100");
myWindow.onload = function() {
alert(myWindow.document.getElementById("result").innerHTML);
myWindow.close();
};
}
关于javascript - 无法获取 window.open() 的innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44144466/