我有一个 html 页面,我需要将数据(特别是文本和 id)从某个 html 段落发送到使用 window.open(""); 在 js 文件中创建的子页面。
我已经尝试将脚本从 js 文件移动到脚本标签中。我尝试使用 .innerText 和 .innerHTML,都返回 Untitled,id 字段没有完全显示。
function newWindow(){
var nuovaFin = window.open("");
var title = opener.document.getElementById("titleHere").innerHTML;
var id = opener.document.getElementById("idHere").id;
nuovaFin.document.write("<html><title> "+ title +" </title><body> <p> "+ id +"</p> </body></html>");
};
<p id="titleHere" >ProvaText</p>
<p id="idHere" >saas</p>
<input type="button" id="btnNew" onclick="newWindow();"> test </input>
我希望 var title 显示为新创建页面的标题,正如它应该的那样,位于 document.write 参数之后,但它显示“Untitled”,并且该段落中的 id 根本不显示.
最佳答案
我对代码做了一些更改,从标题和 id 值中删除了 opener,它只是通过“document.getElementById”运行,不需要“opener.document.getElementById”。 希望它会有所帮助。
function newWindow(){
var nuovaFin = window.open("");
var title = document.getElementById("titleHere").innerHTML;
var id = document.getElementById("idHere").id;
nuovaFin.document.write("<html><title> "+ title +" </title><body> <p> "+ id +"</p> </body></html>");
};
</script>
<p id="titleHere" >ProvaText</p>
<p id="idHere" >saas</p>
<input type="button" id="btnNew" onclick="newWindow();"> test </input>
关于javascript - 我需要让 JS 创建的页面显示父窗口中的一些代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376669/