删除CSS @media打印规则而无需修改CSS文件 [英] Remove CSS @media print rules without modifying CSS files

查看:344
本文介绍了删除CSS @media打印规则而无需修改CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将引导CSS文件添加到页面中。我不想修改CSS文件。

I add bootstrap CSS files to my page. I don't want to modify the CSS files.

当我尝试打印页面时,引导程序修改媒体以打印样式并删除所有颜色属性。

When I try yo print page, bootstrap modify media to print styles and remove all color properties.

如何取消用于打印的引导媒体设置?

How can I cancel bootstrap media settings for print?

此jquery代码可以删除所有CSS; $( *)。css( all, unset);

This jquery code can remove all css; $("*").css("all","unset");

是否可以删除仅@media display {..}

Is there way to remove css settings for only @media display{ .. }

推荐答案

您可以获取样式表并删除以'@ media print',请注意,应将 styleSheets [0] 指向带有相关规则的样式表。

You could get the stylesheet and remove each rule that starts with '@media print', note that styleSheets[0] should be pointed to the stylesheet with the relevant rules.

for(var i=document.styleSheets[0].rules.length -1; i >0; i--){
   if(document.styleSheets[0].rules[i].cssText.indexOf("@media print") !=-1 )
   {
      document.styleSheets[0].deleteRule(i);
   }
}

免责声明:这是臭代码,我不会使用在实际应用中。我会寻找合适的解决方案(例如,从css文件中删除行)
但是,有时您无法控制正在提供的所有文件,或者只需要打印页面而无需进行其他格式化。这样就足够了。

disclaimer: it's smelly code, I would not use this in an actual application. I would look for an appropriate solution (i.e. removing the lines from the css file) However, sometimes you don't have control over all the files being served, or you just need to print a page without additional formatting. Then this would suffice.

这篇关于删除CSS @media打印规则而无需修改CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆