Javascript 使用 jQuery 删除 CSS 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11683307/
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-24 06:43:13  来源:igfitidea点击:

Removing CSS property using jQuery

javascriptjquery

提问by Willem de Wit

Possible Duplicate:
Is it possible to remove inline styles with jQuery?

可能的重复:
是否可以使用 jQuery 删除内联样式?

I'm building a javascript application doing something with images.

我正在构建一个处理图像的 javascript 应用程序。

At some point I'm having an element with inline style css-properties (width & height). I want to remove these properties with javascript/jQuery. I know you can set css-properties with .css() method.

在某些时候,我有一个带有内联样式 css-properties(宽度和高度)的元素。我想用 javascript/jQuery 删除这些属性。我知道您可以使用 .css() 方法设置 css-properties。

Is there a way to remove the properties like you do with .removeAttr()? Potential other css-properties should not be removed.

有没有办法像使用 .removeAttr() 那样删除属性?不应删除潜在的其他 css 属性。

回答by Shreedhar

try this :

尝试这个 :

$('div').css({'width' : '', 'height' : ''});

or if there is a class, you remove it by:

或者如果有一个类,您可以通过以下方式将其删除:

$('div').removeClass('someClass');

回答by Engineer

From the documentation:

文档

Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '')— removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element.

将样式属性的值设置为空字符串 - 例如$('#mydiv').css('color', '')- 如果该属性已被直接应用,则从元素中删除该属性,无论是在 HTML 样式中属性,通过 jQuery 的 .css() 方法,或通过样式属性的直接 DOM 操作。但是,它不会删除已在样式表或元素中应用 CSS 规则的样式。

So what you need, is just this:

所以你需要的是:

$('element').css({width:'', height:''});