javascript - 如何为使用window.open打开的新窗口指定外部js和css

标签 javascript jquery html

我正在尝试在新选项卡或新窗口中打开一些内容。我正在尝试的代码是

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/

相关文章:

Javascript OpenWindow 不加载样式表不工作

javascript - 如何使用 php 或 javascript/jquery 脚本从服务器下载 .PHP 文件

javascript - rivets.js 试图让自定义适配器工作

javascript - 在 Mobile Angular Ui 中隐藏特定页面的侧边栏。(Angular Js)

javascript - 如何清除 MVC 应用程序中的浏览器缓存?

javascript - 列表的 HTML5 session 存储

javascript - 将 div 放置在包含 div 的底部

javascript - 提交按钮上的单击事件中的 AJAX

html - 是否有相当于背景大小 : cover and contain for image elements?

javascript - 将数据填充到我所有的跨度中