如何解决如何将CSS文件添加到页面正文?
| 我正在使用一个具有打印视图的页面,因此当用户单击\“打印\”时,会弹出一个新窗口,我想为该窗口添加一个.css文件。 谢谢解决方法
$(document).ready(function () {
$(\".print\").click(function () {
$(\"head\").append(\"<link>\");
css = $(\"head\").children(\":last\");
css.attr({
rel: \"stylesheet\",type: \"text/css\",href: \"css/ecsPrint.css\"
});
return false;
});
});
除非这是一个内部Web应用程序,否则我不建议使用JS加载CSS。
,包含样式表时,请使用以下命令:
<link rel=\"stylesheet\" media=\"print\" href=\"stylesheet.css\" />
,您可以添加仅打印样式表
<link rel=\"stylesheet\" type=\"text/css\" media=\"print\" href=\"print.css\" />
无需在弹出窗口中“添加”该样式表。当用户处于““普通”视图时,它将被忽略,然后仅在用户要打印文档时才应用。
如果要同时可见并应用于打印的文档,只需将其层叠即可。
<link rel=\"stylesheet\" type=\"text/css\" href=\"normal.css\" /> #normal styling
<link rel=\"stylesheet\" type=\"text/css\" href=\"print.css\" /> #override normal styling
<link rel=\"stylesheet\" type=\"text/css\" media=\"print\" href=\"print.css\" /> #override normal,for printer
这样一来,您可以两全其美。根据需要,用print.css
中的新规则覆盖normal.css
中的css规则,以使文档看起来像您希望的那样。新文档的外观将有所不同,然后也将以不同的方式打印。
,编辑:由于澄清而删除。新答案:
像这样在新窗口中打开URL:blah.aspx?print=true
然后在您的后端:
//Assuming you use .NET...
if(Request.Querystring(\"print\").toLower() == \"true\" ) { //Convert to bool type if you are so inclined
printCSS.Visible = True;
}
HTML:
<head>
<link rel=\"stylesheet\" type=\"text/css\" href=\"...\" visible=\"false\" runat=\"server\" id=\"printCSS\" />
</head>
您也可以使用所示的jQuery方法之一来使用jQuery加载它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。