使用javascript将DIV添加到弹出窗口中

Add DIV to popup window with javascript

本文关键字:窗口 添加 javascript DIV 使用      更新时间:2023-09-26

所以我有一个包含CSS的HTML文档,它定义了DIV页面的布局。我想打开一个新窗口并将 DIV 添加到此窗口,其中包含来自数组的信息。document.write将不起作用,因为它会擦除现有页面,从而删除CSS。我试过newwindow.document.body.appendChild(),但什么都没有出现。我错过了什么?

编辑:好的,因为事实证明我的代码在IE中按预期工作,但在我测试它的Chrome中却没有。这似乎是一种安全措施,可以关闭它。

var printWindow = window.open("widgets/eSearchPlus/this_is_a_test.html", "PrintLabels", "toolbar=yes, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));
    var div = document.createElement('div');
    div.className = "label";
    div.innerHTML = "test";
    printWindow.document.body.appendChild(div);

试试这个:

var printWindow = window.open("widgets/eSearchPlus/this_is_a_test.html", "PrintLabels", "toolbar=yes, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));
printWindow.document.body.innerHTML += '<div class="label">test</div>';

您的问题可能是您在父窗口中创建一个元素并尝试将其添加到子窗口中。

尝试更改要在子窗口文档中创建的元素:

var div = printWindow.document.createElement('div');