Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何编辑<;头>;当我使用window.open打开新窗口时_Javascript_Reactjs - Fatal编程技术网

Javascript 如何编辑<;头>;当我使用window.open打开新窗口时

Javascript 如何编辑<;头>;当我使用window.open打开新窗口时,javascript,reactjs,Javascript,Reactjs,我正在尝试打开一个新窗口,并为我正在制作的报告传入一些生成的html 我已经精简了我的源代码,但下面是我的问题的一个工作示例 var windowUrl = "/"; var uniqueName = new Date(); var windowName = "Print" + uniqueName.getTime(); var printWindow = window.open( windowUrl, windowName,

我正在尝试打开一个新窗口,并为我正在制作的报告传入一些生成的html

我已经精简了我的源代码,但下面是我的问题的一个工作示例

    var windowUrl = "/";
    var uniqueName = new Date();
    var windowName = "Print" + uniqueName.getTime();
    var printWindow = window.open(
        windowUrl,
        windowName,
        "left=0,top=0,width=500,height=500"
    );

    var link = printWindow.document.createElement('link');
    link.rel = 'stylesheet';
    link.href = '//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css';
    printWindow.document.head.appendChild(link);
    printWindow.document.write('<div><section id="divToPrint"><div class="container page-sub">');
    printWindow.document.write('<p>Hello World</p>');
    printWindow.document.write('</div></section></div>');
    printWindow.document.close();
    printWindow.focus();
var windowUrl=“/”;
var uniqueName=新日期();
var windowName=“Print”+uniqueName.getTime();
var printWindow=window.open(
windowUrl,
windowName,
“左=0,顶=0,宽=500,高=500”
);
var link=printWindow.document.createElement('link');
link.rel='stylesheet';
link.href='//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css';
printWindow.document.head.appendChild(链接);
printWindow.document.write(“”);
printWindow.document.write(“helloworld

”); printWindow.document.write(“”); printWindow.document.close(); printWindow.focus();
write方法工作得很好,我可以将我需要的代码写入DOM,但它只写入主体。现在我需要在头部添加信息,如样式、标题等。。。但是我无法将我的头代码放入头标签中

甚至可以写下新窗口的标题吗?当它输出head标记时,即使在添加链接后也是如此

<head></head>


我不知道它是否相关,但我正在使用ReactJS。也许还有其他选择。

您可以访问窗口的
head
节点,然后使用一些本机JavaScript DOM API:

var link=printWindow.document.createElement('link');
link.href='whatever';
printWindow.head.appendChild(链接);
根据您的代码更新了示例:

var windowUrl=“/”;
var uniqueName=新日期();
var windowName=“Print”+uniqueName.getTime();
var printWindow=window.open(
windowUrl,
windowName,
“左=0,顶=0,宽=500,高=500”
);
printWindow.document.write(“”);
printWindow.document.write(“helloworld

”); printWindow.document.write(“”); printWindow.document.close(); printWindow.focus(); var link=printWindow.document.createElement('link'); link.rel='stylesheet'; link.href='//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css'; printWindow.document.head.appendChild(链接);
这看起来就像我期望的头标签的样子。您应该提供一个。我认为您可以使用printWindow.document对象作为主元素。JQuery:$(printWindow,“head”).html(“hi”)@Quentin我添加了一个更好的代码示例,我已经测试过了,并且确实演示了这个问题。这有助于你解决我的问题吗?复制:-你正在修改头部,然后使用document.write将其销毁。@Quentin不要误会,但我认为你误解了复制的含义。这个问题与您链接的问题完全不同。它甚至不能解释为什么会这样。我在示例中使用write 3次,并且所有3行都出现在正文中。也就是说,你在评论中有效地回答了我的问题,告诉我我需要最后一次写信给负责人。这非常有用,并且有效,我感谢你的回答。如果你想把它作为一个答案,我很乐意为其他任何遇到这个问题的人标记它是正确的。这仍然不会写在头上。您的代码实际上没有运行,因为printWindow没有head属性或createElement函数,而是文档属性中有head。我对我的问题进行了编辑,以包含一个最小、完整且可验证的示例缺少
。文档
是一个打字错误。如果新文档中没有
标题
标记,您可以创建它。。。错误是您正在使用文档.close()之前的API
。我更新了基于你的例子。非常感谢更新的答案。把头放在后面很好。不过,您可能希望编辑它,并明确指出不同之处,因为有时人们不会阅读注释,因此如果其他人遇到此问题,它会更有用。再次感谢