How do I unset an element's CSS attribute using jQuery?

If I set a CSS value on a specific element using:

$('#element').css('background-color', '#ccc');

I want to be able to unset that element-specific value and use the cascaded value, along the lines of:

$('#element').css('background-color', null);

But that syntax doesn't seem to work -- is this possible using another syntax?

Thanks in advance!

Edit: The value isn't inherited from the parent element -- the original values comes from an element-level selector. Sorry for any confusion!


From the jQuery docs:

Setting the value of a style property to an empty string — eg $('#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 <style> element.


I think you can also do:

$('#element').css('background-color', '');

That's what I used to do a long time ago for the display property in plain-old-javascript to show/hide a field.


Actually, the syntax I thought was incorrect (with null ) seems to be working -- my selector was just improperly formed, and thus yielding no elements. D'oh!

链接地址: http://www.djcxy.com/p/16992.html

上一篇: 使用JQuery在类中更改CSS规则

下一篇: 如何使用jQuery取消元素的CSS属性?