GetComputedStyle到屏幕以外的其他媒体

GetComputedStyle to other media than screen

本文关键字:其他 媒体 屏幕 GetComputedStyle      更新时间:2023-09-26
@media screen{    
    a {color:green}
}
@media print{
    a {color:red}
}

是否有一种方法,使用JavaScript,得到"a"元素的颜色,当它被打印?正常的getComputedStyle只适用于screen介质

我不想读取style元素。我需要computed style

我开发了一个解决方案,如何删除所有的样式打印媒体。这样,打印样式将与SCREEN样式相同。

这是为了使打印机打印出来的东西和屏幕上显示的一模一样。

console.debug('styleSheetsLength')
                $('link[media=print]').remove()
                $('link[media*=screen]').attr('media','print,screen')
                for (var x=0; x<document.styleSheets.length;x++){
                    console.groupCollapsed('styleSheet-' + x,document.styleSheets[x])
                    if(document.styleSheets[x].rules){
                        for (var i=0; i<document.styleSheets[x].rules.length;i++) {
                            console.groupCollapsed('rule-(' + x + ')-' +i +  document.styleSheets[x].rules[i])
                            console.debug(document.styleSheets[x].rules[i])
                            if(document.styleSheets[x].rules[i].media){
                                for(var j=0;j<document.styleSheets[x].rules[i].media.length;j++){
                                    if (document.styleSheets[x].rules[i].media[j].indexOf('print')>-1)
                                        document.styleSheets[x].rules[i].media.mediaText='nothing'
                                    if (document.styleSheets[x].rules[i].media[j].indexOf('screen')>-1)
                                        document.styleSheets[x].rules[i].media.mediaText='print,screen'
                                }
                            }
                            console.groupEnd();
                        }
                    }
                    console.groupEnd()
                }