--- title: CSSStyleDeclaration slug: Web/API/CSSStyleDeclaration translation_of: Web/API/CSSStyleDeclaration ---

{{ APIRef("CSSOM") }}

概要

CSSStyleDeclaration 表示了一個 CSS 屬性名值對(property-value pairs)的集合。它被用於幾個 API 當中:

屬性

{{domxref("CSSStyleDeclaration.cssText")}}
Textual representation of the declaration block. Setting this attribute changes the style.
{{domxref("CSSStyleDeclaration.length")}} {{readonlyInline}}
The number of properties. See the {{domxref("CSSStyleDeclaration.item", 'item()')}} method below.
{{domxref("CSSStyleDeclaration.parentRule")}} {{readonlyInline}}
The containing {{domxref("CSSRule")}}.

方法

{{domxref("CSSStyleDeclaration.getPropertyPriority()")}}
Returns the optional priority, "important".
{{domxref("CSSStyleDeclaration.getPropertyValue()")}}
Returns the property value given a property name.
{{domxref("CSSStyleDeclaration.item()")}}
Returns a property name.
{{domxref("CSSStyleDeclaration.removeProperty()")}}
Removes a property from the CSS declaration block.
{{domxref("CSSStyleDeclaration.setProperty()")}}
Modifies an existing CSS property or creates a new CSS property in the declaration block/.
{{domxref("CSSStyleDeclaration.getPropertyCSSValue()")}} {{obsolete_inline}}
Only supported via getComputedStyle in Firefox. Returns the property value as a {{ domxref("CSSPrimitiveValue") }} or null for shorthand properties.

範例

var styleObj = document.styleSheets[0].cssRules[0].style;
console.log(styleObj.cssText);

for (var i = styleObj.length; i--;) {
  var nameString = styleObj[i];
  styleObj.removeProperty(nameString);
}

console.log(styleObj.cssText);

備註

The declaration block is that part of the style rule that appears within the braces and that actually provides the style definitions (for the selector, the part that comes before the braces).

參見

規範

Specification Status Comment
{{SpecName('CSSOM', '#the-cssstyledeclaration-interface', 'CSSStyleDeclaration')}} {{Spec2('CSSOM')}}  
{{SpecName('DOM2 Style', 'css.html#CSS-CSSStyleDeclaration', 'CSSPrimitiveValue')}} {{Spec2('DOM2 Style')}} Initial definition