CSSStyleDeclaration - Web APIs 编辑
The CSSStyleDeclaration
interface represents an object that is a CSS declaration block, and exposes style information and various style-related methods and properties.
A CSSStyleDeclaration
object can be exposed using three different APIs:
- Via
HTMLElement.style
, which deals with the inline styles of a single element (e.g.,<div style="...">
). - Via the
CSSStyleSheet
API. For example,document.styleSheets[0].cssRules[0].style
returns aCSSStyleDeclaration
object on the first CSS rule in the document's first stylesheet. - Via
Window.getComputedStyle()
, which exposes theCSSStyleDeclaration
object as a read-only interface.
Attributes
CSSStyleDeclaration.cssText
- Textual representation of the declaration block. Setting this attribute changes the style.
CSSStyleDeclaration.length
Read only- The number of properties. See the
item()
method below. CSSStyleDeclaration.parentRule
Read only- The containing
CSSRule
.
CSS Properties
CSSStyleDeclaration.cssFloat
- Special alias for the
float
CSS property. CSSStyleDeclaration
named properties- Dashed and camel-cased attributes for all supported CSS properties.
Methods
CSSStyleDeclaration.getPropertyPriority()
- Returns the optional priority, "important".
CSSStyleDeclaration.getPropertyValue()
- Returns the property value given a property name.
CSSStyleDeclaration.item()
- Returns a CSS property name by its index, or the empty string if the index is out-of-bounds.
- An alternative to accessing
nodeList[i]
(which instead returnsundefined
wheni
is out-of-bounds). This is mostly useful for non-JavaScript DOM implementations. CSSStyleDeclaration.removeProperty()
- Removes a property from the CSS declaration block.
CSSStyleDeclaration.setProperty()
- Modifies an existing CSS property or creates a new CSS property in the declaration block.
CSSStyleDeclaration.getPropertyCSSValue()
- Only supported via getComputedStyle in Firefox. Returns the property value as a
CSSPrimitiveValue
ornull
for shorthand properties.
Example
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);
Specifications
Specification | Status | Comment |
---|---|---|
CSS Object Model (CSSOM) The definition of 'CSSStyleDeclaration' in that specification. | Working Draft | Merged the DOM Level 2 Style CSS2Properties interface into CSSStyleDeclaration . |
Document Object Model (DOM) Level 2 Style Specification The definition of 'CSSStyleDeclaration' in that specification. | Obsolete | Initial definition |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论