3

我正在使用 Greasemonkey 和 JQuerys #css 方法将 css 样式添加到页面。到目前为止的脚本:

// ==UserScript==
// @name           www.al-anon.dk Remove inline scroll so that page content prints properly
// @namespace      http://userscripts.org/users/103819
// @description    remove scroll from al-anon pages
// @include        http://al-anon.dk/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

$('#framen').css({'height': 'auto', width: 'auto'});
$('#menu').css({ 'display': 'none'});

现在我的问题是如何仅将最后一条规则应用于@media print?

换句话说:如果这是干净的 CSS,我将使用以下语法:

@media print {
  /* style sheet for print goes here */
}

但是如何使用 Greasemonkey/JQuery 来选择它

4

2 回答 2

3

您可以附加一个样式元素:

$('<style media="print">#menu {display: none;}</style>').appendTo('head');
于 2009-11-06T10:08:49.317 回答
3
GM_addStyle('@media print { #menu { display:none; } }');

此外,如果您希望您的脚本在其他浏览器中运行。

/**
 * Define GM_addStyle function if one doesn't exist
 */
if( typeof GM_addStyle != 'function' )
function GM_addStyle(css)
{
    var style = document.createElement('style');
    style.innerHTML = css;
    style.type='text/css';
    document.getElementsByTagName('head')[0].appendChild(style);
}
于 2009-12-22T01:31:58.057 回答