Javascript 将整个网页放入document.write()中;

Javascript 将整个网页放入document.write()中;,javascript,html,Javascript,Html,我不能用我的代码链接到另一个弹出窗口页面,因此我必须使用document.write,但当我单击按钮时,它甚至不会打开弹出窗口。 例如,此代码可以工作: <button onclick="myFunction()">Try it</button> <script> function myFunction() { var myWindow = window.open("", "myWindow", "width=200,height=100"); myWindo

我不能用我的代码链接到另一个弹出窗口页面,因此我必须使用document.write,但当我单击按钮时,它甚至不会打开弹出窗口。 例如,此代码可以工作:

<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<script>alert('test')<\/script>");
myWindow.document.close();
var p = document.createElement("p")
p.innerHTML = "This is the source window!";
opener.document.querySelector("body").appendChild(p)
}
</script>
试试看
函数myFunction(){
var myWindow=window.open(“,”myWindow“,”宽度=200,高度=100”);
myWindow.document.write(“警报('test')”);
myWindow.document.close();
var p=document.createElement(“p”)
p、 innerHTML=“这是源窗口!”;
opener.document.querySelector(“body”).appendChild(p)
}
但是,当我添加诸如分隔符之类的元素时,它不起作用。例如:

<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<div>
<div id="test">
<p>Test</p>
<br>
</div>
</div>");
myWindow.document.close();
var p = document.createElement("p")
p.innerHTML = "This is the source window!";
opener.document.querySelector("body").appendChild(p)
}
</script>
试试看
函数myFunction(){
var myWindow=window.open(“,”myWindow“,”宽度=200,高度=100”);
myWindow.document.write(“
试验


"); myWindow.document.close(); var p=document.createElement(“p”) p、 innerHTML=“这是源窗口!”; opener.document.querySelector(“body”).appendChild(p) }
有两个问题:

1你用错了双引号
myWindow.document.write(“
应该是这样

myWindow.document.write("<div>
<div id='test'>
myWindow.document.write("<div><div id="test"><p>Test</p>....
myWindow.document.write(“
2在JavaScript中,不能使用双引号换行, 这是错误的

myWindow.document.write("<div>
<div id="test">
<p>Test</p>
myWindow.document.write(“
试验

应该是

myWindow.document.write("<div>
<div id='test'>
myWindow.document.write("<div><div id="test"><p>Test</p>....
myWindow.document.write(Test

)。。。。
最后

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
var myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<div><div id='test'><p>Test</p><br></div></div>");
myWindow.document.close();
var p = document.createElement("p")
p.innerHTML = "This is the source window!";
opener.document.querySelector("body").appendChild(p)
}
</script>
试试看
函数myFunction(){
var myWindow=window.open(“,”myWindow“,”宽度=200,高度=100”);
myWindow.document.write(“测试”


”; myWindow.document.close(); var p=document.createElement(“p”) p、 innerHTML=“这是源窗口!”; opener.document.querySelector(“body”).appendChild(p) }

如果你想用JS编写html,请参阅。你知道一个有效的html文档有DTD、html、head和body元素,head代表元内容,就像链接样式表一样,body是显示实际内容的地方。所有这些元素都必须在尝试使用它们之前创建。是的,这不是我的完整代码,只是显示part我需要帮助。如果有帮助,实际的弹出窗口将不会打开。添加这些元素仍然不起作用。它只是无法打开。在双引号JavaScript字符串中不能有这样的文字新行。请参阅。您可以通过检查控制台来发现这样的错误。此外,您还需要转义字符串中的引号(例如,在
中)。即使我们消除了上面代码中的语法错误,也不确定它是否仍然有效。您打开一个新窗口,用html填充它,然后立即关闭它。您的第一个示例将显示该窗口,因为您使用了警报,这将阻止脚本执行。