我正在尝试使用 ajax 调用打开一个包含检索到的 View 及其数据的新浏览器窗口。
$.ajax({
type: 'POST',
url: redirectToANewWindow,
dataType: 'html',
data: { filterValue: searchValue },
success: function (result) {
var NewBrowserWindow = window.open('', '_blank', 'height=500,width=900,scrollbars=yes,location=no');
$(NewBrowserWindow.document.body).html(result);
}
});
字段及其数据显示在新打开的浏览器上。但是返回的 View 没有 CSS,所以所有的字段都是乱七八糟的。我的主要 CSS 在其他地方声明,出于某种原因,我无法在返回的 View 中声明它。我的问题是,如何返回此 View 并附加一个 CSS 文件?
我试过了,
success: function (result) {
var styles = "<link rel='stylesheet' href='../Content/GlobalStyle.css' />";
var NewBrowserWindow = window.open('', '_blank', 'height=500,width=900,scrollbars=yes,location=no');
$(NewBrowserWindow .document.head).html(styles);
}
但是它不起作用,尽管我可以看到在 html 上添加了 CSS 引用。是不是就是找不到我引用的文件?
顺便说一句,我正在使用 MVC。
最佳答案
试试这段代码:
$('<link/>', {
rel: 'stylesheet',
type: 'text/css',
href: '../Content/GlobalStyle.css'
}).appendTo('Head');
关于jquery - 在新打开的浏览器窗口上附加 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16453024/