我正在尝试在新选项卡或新窗口中打开一些内容。我正在尝试的代码是
var w = window.open();
$(w.document.head).html("<link rel='stylesheet' href='style.css'/>");
$(w.document.body).html(data);
其中 data 表示要放置在新窗口上的 html 内容。问题是,当打开新窗口时,所有内容都会出现,但其样式丢失。检查 head 中的标签时显示未定义。如何在新窗口中以正确的样式显示数据?
最佳答案
将 *** 替换为您的 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();
请告诉我这是否有用:)
关于javascript - 如何为使用window.open打开的新窗口指定外部js和css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641268/