javascript - 如何编写对 window.open 函数的元素引用?
问题描述
所以,我开始在 javascript 中使用“window.open”,很快就被卡住了。
在我的示例代码中,有一个 id 为“reference”的 ap 标记,以及一个调用函数的按钮,该函数旨在将该 p 标记写入新选项卡。
我将如何正确引用 p 标签并将其写入新标签?
到目前为止,我只使用下面的代码获得了“[object HTMLParagraphElement]”......
HTML
<p id="reference">This is some placeholder text meant for another page.</p>
<button onclick="createTab()">Create Tab</button>
JavaScript
function createTab() {
var myWindow = window.open("", "Test");
myWindow.document.write(document.getElementById("reference"));
}
解决方案
我认为您可以使用以下内容代替 document.write:
myWindow.document.appendChild(document.getElementById("reference"))
希望能解决您的问题。
推荐阅读
- jwt - Quarkus:集成测试 - 如何模拟 OIDC?
- angular - RROR 错误:找不到带有路径的控件:formArray
- python - 如何使用 python 或 pyspark 获取带有年份的周数列表(此格式为 202114)
- discord.js - 错误提供的选项不是对象。在 Message.delete
- html - 页面转向 http 而不是 https 并且不加载导航栏
- sql - 用于 SQL 查询的弹性搜索查询
- java - 从 int[] 到 int 的不兼容转换
- javascript - 我怎样才能让我的 Discord 机器人在被提及时识别它?
- python - IndentationError: unindent does not match any external indentation level 帮我解决这个问题?
- c# - 玩家与对象碰撞后如何再次重生对象?