如何使用window.open

时间:2015-06-04 10:22:08

标签: javascript jquery html

我正在尝试在新标签页或新窗口中打开一些内容。我正在尝试的代码是

var w = window.open();

$(w.document.head).html("<link rel='stylesheet' href='style.css'/>");

$(w.document.body).html(data);

其中data表示要放在新窗口中的html内容。问题是当打开新窗口时,所有内容都会出现,但缺少其样式。检查头部标签时显示未定义。如何以适当的样式在新窗口中显示数据?

4 个答案:

答案 0 :(得分:1)

用你的html代码替换***

var w = window.open();
w.document.write('<html><head><title>Test</title>');
w.document.write('<link rel="stylesheet" href="style.css">');
w.document.write('</head><body>');
w.document.write('<p>This is the new page.</p>');
w.document.write('</body></html>');
w.document.close();

让我知道这是否有用:)

答案 1 :(得分:0)

如果你正在使用html,你可以将目标设置为空白,这将在新窗口中打开链接:

  $("#amount_paid' + data[i].patient_visit_statement_id + '").keyup(function() {
       var amount_owed = $('#amount_owed' + data[i].patient_visit_statement_id + '').val();
       var amount_paid = $('#amount_paid' + data[i].patient_visit_statement_id + '').val();
       alert('Amount owed' + amount_owed + 'Amount paid' + amount_paid + '');
       if (amount_owed < amount_paid) {
           alert('Amount to be paid exceeds amount owed');
       }
   });

希望这有帮助

答案 2 :(得分:0)

除非新窗口的内容必须由第一个窗口创建,否则在客户端,更容易使用由服务器创建并在window.open()下命名为参数的页面 - 如此:

window.open ('/popup-content.html');

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

答案 3 :(得分:0)

以下stackoverflow答案Add css file with jquery介绍了如何使用追加将外部文件添加到 head 元素。

所以在你的情况下,像

$(w.document.head).append("<link rel='stylesheet' href='style.css' type='text/css'/>");

应该有效