如何使用 CSS 删除类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4215412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:12:29  来源:igfitidea点击:

How to remove class using CSS

css

提问by Amra

I need to remove classes from different elements within the @media printon my CSS and add those classes on the @media screen.

我需要从@media printCSS 中的不同元素中删除类,并将这些类添加到@media screen.

Is there anyway to remove classes from the CSS file?

有没有办法从 CSS 文件中删除类?

Something like:

就像是:

@media print{
    .element{/*Remove class*/}
}
@media screen{
    .element{/*Add class*/}
}

I need to remove ui-tabs-hideadded by a jQuery function (which hides the element in a weird way, as its not display:blockor display:none) class from those elements at the time of printing and putting it back when finished printing.

我需要在打印时从这些元素中删除ui-tabs-hide由 jQuery 函数添加的(它以一种奇怪的方式隐藏元素,因为它不是display:blockdisplay:none)类,并在打印完成后将其放回原处。

回答by Quentin

No. CSS can't modify the DOM, only its presentation.

不。CSS 不能修改 DOM,只能修改它的表示。

回答by Kyle

CSS can't take out elements from the HTML document, however you could try something like:

CSS 无法从 HTML 文档中取出元素,但是您可以尝试以下操作:

@media print{
element.relevantclass
   {
     display: none;
   }

This would tell the printed media to not display this element.

这将告诉印刷媒体不要显示此元素。

回答by Achintha

you can use jQuery to do this.

你可以使用 jQuery 来做到这一点。

$(<element>).removeClass() // or removeClass(<the class to be removed>)
$(<element>).addClass(<new class>)

if you want to do this on loading of the page, you can include this in the document ready function (as shown below)

如果您想在页面加载时执行此操作,可以将其包含在文档就绪功能中(如下所示)

$( document ).ready(function() {
    $(<element>).removeClass()
    $(<element>).addClass(<new class>)
});